Python print()
Function
Description
The print() method is used for printing output, one of the most common functions.
The flush
keyword argument was added in Python3.3.
>
In Python3.x, print
is a function, but in Python2.x, it is not a function but a keyword.
Syntax
Here is the syntax for the print() method:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
Parameters
objects -- plural, indicating that multiple objects can be output at once. Multiple objects need to be separated by a comma.
sep -- used to separate multiple objects, default is a space.
end -- used to set what ends the output. The default is the newline character
\n
, but it can be replaced with other strings.file -- the file object to write to.
flush -- whether the output is buffered usually depends on the file, but if the
flush
keyword argument is True, the stream will be forcefully flushed.
Return Value
None.
Examples
Below are examples of using the print function:
Test in Python3
>>> print(1)
1
>>> print("Hello World")
Hello World
>>> a = 1
>>> b = 'tutorialpro'
>>> print(a,b)
1 tutorialpro
>>> print("aaa""bbb")
aaabbb
>>> print("aaa","bbb")
aaa bbb
>>>
>>> print("www","tutorialpro","com",sep=".") # Set separator
www.tutorialpro.org
Using the flush
parameter to create a Loading effect:
Example
import time
print("---tutorialpro EXAMPLE: Loading Effect---")
print("Loading",end = "")
for i in range(20):
print(".",end = '',flush = True)
time.sleep(0.5)
The effect is shown in the following image:
More content can be found at: Python3 print Function Usage Summary