Print messages to stdout
print(*[message][, sep=][,end=])
You can print to stdout in various ways
sep=
: Separator for argumets to build the stringend=
: End charactef for the stingfile=
: File object to redirect toflush
: Avoid buffering of text until a new line character
You can use the sep
and end
arguments to define a separator for
your string and a line end. The default is " "
and \n
respectively. But you can change it to any arbitrary sting
To print different calls to print
in the same line, change the end
parameter to ''
print('Mercury', 'Venus', 'Earth', sep=', ', end=', ')
print('Mars', 'Jupiter', 'Saturn', sep=', ', end=', ')
print('Uranus', 'Neptune', 'Pluto', sep=', ')
# Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto
There are three basic standard streams:
- stdin: Represented by
0
- stdout: Represented by
1
- stderr: Represneted by
2
So to write to a file you pass a file object to the file
parameter
with open('file.txt', mode='w') as file_object:
print('hello world', file=file_object)
You can mock a file to redirect by writting directly to memory with the io module
import io
fake_file = io.StringIO()
print('hello world', file=fake_file)
fake_file.getvalue() # 'hello world\n'
By default print
will only print text to the screen after a new line
character is encountered.
This is undesirable if you want to print non \n
text to the screen
sequentialy. To avoid that pass the flsh=True
argument to forcefully flush
the stream without waiting for a newline character in the buffer
import time
num_seconds = 3
for countdown in reversed(range(num_seconds + 1)):
if countdown > 0:
print(countdown, end='...', flush=True)
time.sleep(1)
else:
print('Go!')