print() Function and its Use in Python Program
Input and Output in Python
In this lesson, we will understand what print() function is and how it works in Python programming with the help of some examples.
What is print() Function
The print() function is used in the Python program to print text as well as the value of the variables on the screen.
Let's see the use of print() function in python with the help of some examples.
Example 1
Python program to print Hello World text on the screen.
print('Hello World')
print("Hello World")
Output
Hello World Hello World
Here you can see that we have printed the text Hello World on the screen using print() function. So whatever you write inside the brackets ( ) of print function it will be printed on the screen. In place of single quotes you can use double quotes as shown above on line number 2.
Example 2
Python program to print Sunday and Monday on separate lines on the screen.
print('Sunday')
print('Monday')
Output
Sunday Monday
You can see that we have used print() function two time to print Sunday and Monday on separate lines on the screen.
Example 3
Python program to print Sunday and Monday on separate lines using single print() function.
print('Sunday\nMonday')
print('\nSunday\n\n\nMonday')
Output
Sunday Monday Sunday Monday
You can see that we have used single print() function on line number 1 to print Sunday and Monday on separate lines on the screen. We have written \n (escape sequence character for new line) just after Sunday so that after printing Sunday the line will break and Monday will be printed on the next line. You can use more \n to break more lines as shown above on line number 2.
Example 4 (Use of sep (separator) parameter in print() function)
Python program to show the use of sep parameter in print() function.
print('Sunday','Monday',sep='-')
print('18','06','2020',sep='/')
print('is Thursday')
Output
Sunday-Monday 18/06/2020 is Thursday
You can see that we have used sep='-' parameter at the end on line number 1 and within single quotes we have written - this will separate the printed text by - as shown in the output.
The default value of sep parameter is space. You can change it to any text or character you want as shown above in line number 2. If we don't use sep parameter in print() function then space will be printed in place of the comma (,) as shown above in line number 3.
Example 5 (Use of end parameter in print() function)
Python program to show the use of end parameter in print() function.
print('Sunday','Monday',end='-')
print('Tuesday')
print('Wednesday')
Output
Sunday Monday-Tuesday Wednesday
The end parameter is used to print the output of multiple print() functions in a single line on the screen.
You can see that we have used end='-' parameter at the end on line number 1 and within single quotes we have written - this will print - at the end of the first line and the output of the second line (Tuesday) will be printed at the end of the first line as shown above in the output screen.
The default value of the end parameter is \n. You can change it to any text or character you want as shown above in line number 1. If we don't use end parameter in print() function then the line will automatically break after printing the text as shown above in line number 2.
Example 6 (Use of sep and end parameters together in print() function)
Python program to show the use of end and sep parameters together in print() function.
print('thomas','wilson',sep='',end='@')
print('dremendo.com')
Output
You can see that we have used sep='' so thomas and wilson will be printed together without any space between them because the sep is blank. At the end of the line we have used end='@' as a result dremendo.com will be printed on the first line after @ character as shown in the output screen.
Example 7
Python program to print integer and float data type values on the screen.
a=10
b=20.32
print('a=',a,' b=',b,sep='',end='\n\n')
print('a=',a,sep='')
print('b=',b,sep='')
Output
a=10 b=20.32 a=10 b=20.32
In the above program, we have printed an integer and a float data type values in two different ways. First, in the same line with a space between them and second on separate lines. The \n\n is used in end parameter to break the first line after printing its output on the screen.
Example 8
Python program to print formatted string on the screen using {} as placeholder and format() function.
a=5
b=1.326149
c='apples'
print('weight of {} {} = {:.2f} kgs'.format(a,c,b))
Output
weight of 5 apples = 1.33 kgs
In the above program, we have printed an integer, float and string data type values on the screen using format() function. In the above program {} are used as placeholders in which data are placed using format() function. Inside format() function we write the variables in the order we want to print on the screen and {:.2f} is used to round up the decimal number up to 2 decimal places as shown in the above example.
Example 9
Python program to print formatted string on the screen using % operator.
a=5
b=1.326149
c='apples'
print('weight of %d %s = %.2f kgs' %(a,c,b))
Output
weight of 5 apples = 1.33 kgs
In the above program, we have printed an integer, float and string data type values on the screen using % operator. In the above program %d, %s and %.2f are used as placeholders to print an integer, a string and a float on the screen respectively. Inside %() we write the variables in the order we want to print on the screen and %.2f is used to round up the decimal number up to 2 decimal places as shown in the above example.
List of Formatting Symbols
Below are the list of symbols used with % operator for formatting outputs on the screen.
Formatting Symbols | Description |
%d | Used for integer printing |
%i | Used for integer printing |
%f | Used for decimal number (float) printing |
%c | Used for character printing |
%s | Used for string (text) printing |
%o | Used for octal number printing |
%x | Used for lowercase hexadecimal number printing |
%X | Used for uppercase hexadecimal number printing |
Escape Sequence Character in Python
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.
In Python, all escape sequences consist of two or more characters, the first of which is the backslash, \ (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence.
Escape Sequence Character | Description |
\n | Insert a linefeed (new line) |
\r | Insert a carriage return |
\f | Insert a form feed |
\b | Insert a backspace |
\t | Insert a tab |
\\ | Insert a backslash |
\" | Insert a double quote |
\' | Insert a single quote |