print() function prints the given string or object to the standard output. The standard output is the screen or to the text stream file.
Example: Print an integer to Screen
print("Learning Python is Easy.") i = 100 print("The value of i is: ", a)
Learning Python is Easy. The value of i is: 100
Syntax of print() Method
The complete syntax of the
print() function is:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
print() method parameters are:
- objects – It takes the objects to the printed in the screen. It can take more than one object as method argument.
- sep – If multiple objects are printed than the objects are separated by
sep. The default value of
- end – It is printed at the last of the output. The default value is new line character (
\n). This is the reason each
print()statement displays the output in the new line.
- file – It must be an object with
write(string)method. If not specified,
sys.stdoutis used which prints output on the screen.
flush– It specifies if the output stream has to be forcibly flushed. Default value is
Falsewhich does not forcibly flush the stream.
flush are the keyword arguments. A keyword argument is passed into method with its name.
Method Return Value
print() method does not return any value. It returns
Example: Keyword arguments in
print("A", "B", "C", "D", sep = ' # ')
A # B # C # D
Python print() Method Examples
Let’s look at few examples to understand how to use the
Example 1: Using print() with
print("A", "B", "C", "D", sep = ' # ', end = ' Done \n')
A # B # C # D Done
Example 2: Using print() with
- The given Python program tries to open the
demo.txtin writing mode.
demo.txtfile doesn’t exist, a new file is created and opened in writing mode.
- The string object
'Hello, Python !'is printed to
demo.txtfile into your computer.
- Do not forget to close the file using
sourceFile = open('demo.txt', 'w') print('Hello, Python !', file = sourceFile) sourceFile.close()
Hello, Python !
Happy Learning !!