Wednesday, July 25, 2012

Python print in the same line successively

The question is like in python code:
for i inrange(1,100):
    print i

And the output is like:
1
2
3
.
.
99

I want it to print in the same line like: 1 2 3 . . . 99
Solution: use ',' to prevent starting from a new line:
for i in range(1,100):
    print i,
Another thing is that if you want to start something from a new line, you should use '\n' in either 'print '\n' ' or '.write('\n')'

No comments:

Post a Comment