The print built-in function is normally called on a line of its own. It is a normal function, so it uses standard function-call syntax. It provides special operation modes with keyword arguments and supports future enhancements better.
Call format
Formal syntax:
print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout][, flush=False])
Items in square brackets are optional and may be omitted in a given call.
sep
is a string inserted between each object’s text, which defaults to a single space
if not passed.end
is a string added at the end of the printed text, which defaults to a\n
newline character if not passed.file
specifies the file, standard stream, or other file-like object to which the text
will be sent; it defaults to thesys.stdout
standard output stream if not passed.flush
was added in Python 3.3, defaults to False . It allows prints to mandate that their text be flushed through the output stream immediately to any waiting recipients. Normally, whether printed output is buffered in memory or not is determined by file ; passing a true value to flush forcibly flushes the stream.
The textual representation of each object to be printed is obtained by passing the object
to the str
built-in call (or its equivalent inside Python). With no arguments at all, the
print function simply prints a newline character to the standard output stream, which usually displays a blank line.
The print
function examples
Printing a variety of object types to the default standard output stream, with the default separator and end-of-line formatting added (these are the defaults).
>>> a = 'Dan'
>>> b = 40
>>> c = ['web', 'developer']
>>> d = (1, 2, 3, 4)
>>> e = {name: 'Dan', age: 40}
Traceback (most recent call last):
File "", line 1, in
NameError: name 'age' is not defined
>>> e = {'name': 'Dan', 'age': 40}
>>> print(a, b, c, d, e)
Dan 40 ['web', 'developer'] (1, 2, 3, 4) {'name': 'Dan', 'age': 40}
By default, print calls add a space between the objects printed. You can send an alternative separator.
>>> print(a, b, c, d, e, sep='|')
Dan|40|['web', 'developer']|(1, 2, 3, 4)|{'name': 'Dan', 'age': 40}
By default, print
adds an end-of-line character to terminate the output line. You can suppress this and avoid the line break by passing an empty string to the end keyword argument, or you can pass a different terminator of your own including a \n character to break the line manually if desired.
>>> print(a, b, c, d, e, sep='|', end='...')
Dan|40|['web', 'developer']|(1, 2, 3, 4)|{'name': 'Dan', 'age': 40}...>>>
>>> print(a, b, c, d, e, sep='|', end='...\n')
>>>
Two statements on one line, separated by a semicolon.
>>> print(a, b); print(a, c)
Dan 40
Dan ['web', 'developer']
>>> print(a, b, end=' ... some more comming ...'); print(a, c)
Dan 40 ... some more comming ...Dan ['web', 'developer']
The file
keyword argument usage.
>>> a = 'Dan'
>>> b = 'Dumitrache'
>>> print(a, b, sep='...', file=open('test.txt', 'w'))
If the file text.txt
does not exist then it will be created.
If you want to read the file then run the following command:
>>> print(open('test.txt').read())
Dan...Dumitrache
Printing the hard way.
>>> sys.stdout.write('hello world\n')
hello world
Manual stream redirection.
It prints not on a terminal but in a specified file.
>>> sys.stdout = open('test.txt', 'a')
>>> print('web developer')
We reset sys.stdout
to open the file named test.txt
, located in the script’s working directory, in append mode (so we add to its current content). After the reset, every print operation anywhere in the program will write its text to the end of the file test.txt
instead of to the terminal.
The sys.stdout
this way will redirect every print anywhere in your program.
In fact, you can even reset sys.stdout
to an object that isn’t a file at all, as long as it has a method named write
to receive the printed text string argument.
Automatic stream redirection.
It is used when you want to go back to terminal printing.
>>> import sys
>>> temp = sys.stdout
>>> sys.stdout = open('test.txt', 'a')
>>> print('Automatic stream redirection')
>>> sys.stdout.close()
>>> sys.stdout = temp
>>> print('Back to terminal printing')
Back to terminal printing
… or …
>>> log = open('test.txt', 'w')
>>> print(1, 2, 3, file = log)
>>> print('a', 'b', 'c', file = log)
>>> log.close()
>>> print(4, 5, 6)
4 5 6
Printing both ways, in the terminal then output to an external file to verify that the same text is printed.
>>> a = 8; b = 10
>>> print(a, b)
8 10
>>> sys.stdout.write(str(a) + ' ' + str(b) + '\n')
8 10
5
>>> print(a, b, file = open('file1', 'w'))
>>> open('file2', 'w').write(str(a) + ' ' + str(b) + '\n')
5
>>> print(open('file1', 'rb').read())
b'8 10\n'
>>> print(open('file2', 'rb').read())
b'8 10\n'