Search code examples
pythonprintingjupyter-notebookseparatorjupyter

Syntax error, print with sep= statement


First and foremost i am very new into Python. I am using a notebook version of Ipython called jupyter and its provided by my University, so I don't know whether this is a standard version or not.

I was busy in a slide course about Python and encountered this exercice:

enter image description here

This is the code I used and the syntax error I get in the Ipython environment

enter image description here

I don't get why it is not working.

Thank you in advance

Olivier


Solution

  • print([object, ...][, sep=' '][, end='\n'][, file=sys.stdout]) is a function in Python 3.x, which has a sep keyword argument (among others).

    If you are using Python 2.7 (try print "Hello!" - if it runs, you have Python 2.x), print is a statement there, which means that if you want to get the behaviour as in your slide (make print a function), you need to import print_function from __future__ module.

    That way you can use print("Hi!", "Hello!", sep='\t') as in your slide.

    As mentioned by @Kevin in his comment below this post, if your course uses Python 3.x, you would be better off to upgrade to this version since things like async, yield from or lzma are not available in Python 2.x.