In python 2, the python print statement was not a function whereas in python 3 this has been turned to into an function
when I type print(
I get some hovertext (or something similar) to
print(value,...,sep=' ', end='\n', file=sys.stdout, flush=False)
I know what value means but a clarification on what what those other variables mean and what are the advantages of python 3's print statement over python 2's would be appreciated (especially sep=' ')
When you provide multiple arguments to print
they usually get separated by a space:
>>> print(1, 2, 3)
1 2 3
sep
lets you change that to something else:
>>> print(1, 2, 3, sep=', ')
1, 2, 3
Normally, print
will add a new line to the end. end
lets you change that:
>>> print('Hello.', end='')
Hello.>>>
Normally print
will write to standard out. file
lets you change that:
>>> with open('test.txt', 'w') as f:
... print("Hello, world!", file=f)
...
Normally print
does not explicitly flush the stream. If you want to avoid an extra sys.stdout.flush()
, you can use flush
. The effect of this is usually hard to see, but trying this without flush=True
should make it visible:
>>> import time
>>> while True:
... print('.', end='', flush=True)
... time.sleep(0.5)
Python 2 doesn't have a sep
equivalent because print
wasn't a function and couldn't be passed arguments. The closest you could do was with join
:
print ' '.join([value, ...])
As for file
, you'd have to use this (awkward, in my opinion) syntax:
print >> sys.stdout, ' '.join([value, ...])
I'm not going to copy/paste the documentation here, so read it if you want to know what those arguments are for.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.