In this tutorial, we will learn about print() function in Python. It is used to print specific data as the output on the screen or output device. You can print a Text, number, variable or an object in Python using a print() method.
Syntax:
print( <variable or string>, sep= <separator value>, end= <end value>, file=file, flush=flush)
Print Example in Python
The print object can be a string or any other object, the object will be converted into a string before printing. You can print one or more objects at a time.
#print Text Value
print("Learning Python")
#print Number
print(10)
#print Multiple Values
print("Python","Tutorial")
#print variable
n=10
print(n)
#print variable with string message
age=input("Enter Your Age")
print("Your Age ",age)
print("Hello, You Are ",age,"Years Old")
Note: In the above example comma (,) in print() is used to separate multiple things to be print.
Using sep in Python Print
When we print multiple string or values using the print() function, all the values are separated with space. However, we can overwrite this with other symbols or any other string. Below you can check different examples of sep keyword in Python.
#print without sep
print("Easy","Python","Tutorial")
#default value of sep in print
print("Easy","Python","Tutorial",sep=' ')
Easy Python Tutorial
Easy Python Tutorial
In the above example, both print statements will show the same output, because the default value of sep is blank space.
#omit space in output, using sep keyword in print
print("Easy","Python","Tutorial",sep='')
#print hyphen(-), between outputs using sep keyword in print
print("Easy","Python","Tutorial",sep='-')
#Another example of sep keyword in print
print("Easy","Python","Tutorial",sep='@')
EasyPythonTutorial
Easy-Python-Tutorial
Easy@Python@Tutorial
Python end Keyword in Print
When we have multiple print statements in a program, Python prints output to a new line. However, we can overwrite this behavior of the print function. We can set white space, hyphen(-), Tab space or any other symbols as ‘end’ of the print statement.
#example 1: default print statement
print("Python")
print("Example")
#example 2: by default end is set to new line
print("Python",end='\n')
print("Example",end='\n')
In the above code, both the example will print the same output. Because by default the end keyword is set to newline(\n), whether you assign ‘\n’ or not.
#if we assign tab character to end
print("Python",end='\t')
print("Example",end='\t')
Python Example
- \n – Special character sequence, print new line.
- \t – Tab character, prints horizontal tabbed space
#if we assign hyphen to end
print("Python",end='-')
print("Example",end='-')
Python-Example-