0

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=' ')

3
  • sep is just the separator between arguments. E.g. print("John", "Doe", sep="\t") prints John Doe. Commented Jun 22, 2013 at 4:38
  • the hover statement is the specification of the print statement. Commented Jun 22, 2013 at 5:41
  • 3
    you can usually always get more info with help(print) Commented Jun 22, 2013 at 5:45

2 Answers 2

5

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.