The print() function is been used to print value in Python. You need to pass your printing variable or value in print().
In this tutorial, I show how you can use print() in Python using some examples.
Contents
1. Printing value
If the value is string type then pass the value between single (‘) or double (“) quotes in print()
function.
Example –
print("Welcome to makitweb") # Print value using double quotes print('Welcome to makitweb') # Print value using single quotes
Output –
Welcome to makitweb Welcome to makitweb
Printing number type value –
If the value is number type then pass it without quotes in print()
function.
Example –
print(2.5) print(43) print(24/4)
Output –
2.5 43 6.0
Add new line –
Use \n
to add a new line.
Example –
print("Hello world!\nLine break text")
Output –
Hello world! Line break text
2. Concat values
You can concat values in 2 ways –
- Using + operator
- Using , operator
1. Using + operator
Specify +
operator between the values which you are printing.
Syntax –
print(value1 + value2 + value + ...)
Example –
print("Yogesh"+"singh")
Output –
Yogeshsingh
2. Using , operator
Specify ,
operator between the values which you are printing.
Syntax –
print(value1, value2, value, ...)
Example –
print("Yogesh","singh")
Output –
Yogesh singh
The comma(,) operator adds space between values while printing.
3. Using with variables
Directly pass variable name in print()
function to print its value without single (‘) or double (“) quotes.
Example –
name = "Yogesh singh" num1 = 20 print(name) print(num1)
Output –
Yogesh singh 20
Contact variables –
Specify multiple variables that are separated by ,
or +
operator. In the example, I am using the comma operator.
Example –
fname = "Yogesh" lname = "Singh" print(fname,lname)
Output –
Yogesh singh
4. Conclusion
Use ,(comma) or +(plus) operator if you want to display related values using single print()
function.