开发者

print last '\n' char using print method in python

开发者 https://www.devze.com 2023-01-28 02:09 出处:网络
Imagine I have the following code in file test.py: for x in range(1,11): print x This will print: $myuser: python test.py

Imagine I have the following code in file test.py:

for x in range(1,11):
     print x

This will print:

$myuser: python test.py
1
2
3
4
5
6
7
8
9
10
$myuser:

However, I want it to print:

$myuser: python t开发者_开发问答est.py
1
2
3
4
5
6
7
8
9
10

$myuser:

Notice the last '\n' char, that is missing in the normal case. How do I get this last newline char?


Nothing is missing; if there wasn't a newline at the end the last line would look like 10$myuser:

Perhaps you want:

for x in range(1,11):
  print x
print


Just put a bare print after the loop.


That's why I love python3:

print(*range(5), sep='\n',end='\n\n')

actually this is ugly:

for i in range(5)+['']:
    print i
0

精彩评论

暂无评论...
验证码 换一张
取 消