A place for my personal musings, about programming, design or whatever come across my mind

Print without newline in Python

Going thru the Head First Python book, which is intended for Python 3.X, although majority of the content work just as well in Python 2.X.

First encountered with some variations between version 3.X and 2.X at the chapter about printing out nested lists, with tab indicating different nested level.

To print a tab without a newline, you could do this in Python 3.X,

print("\t",end='')

However, the same code will throw you a syntax error in Python 2.X.
So to do the same in Python 2.X,

print "\t",

Note the final comma, which actually make sure the line will print out with space instead of a newline.
In Python 3.X, print is an actual function but in Python 2.X, print is still a statement.

I found out this from the ever helpful stackoverflow

1 Comment
Bob
Bob

Nice helped me a lot.

Leave A Comment