打印最后一个'\n' python中使用print方法打印字符
想象一下,我在文件 test.py
中有以下代码:
for x in range(1,11):
print x
这将打印:
$myuser: python test.py
1
2
3
4
5
6
7
8
9
10
$myuser:
但是,我希望它打印:
$myuser: python test.py
1
2
3
4
5
6
7
8
9
10
$myuser:
注意最后一个 '\n' 字符,在正常情况下会丢失。如何获取最后一个换行符?
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 test.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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
什么都不缺;如果末尾没有换行符,最后一行将类似于
10$myuser:
也许您想要:
Nothing is missing; if there wasn't a newline at the end the last line would look like
10$myuser:
Perhaps you want:
只需在循环后放置一个裸露的
print
即可。Just put a bare
print
after the loop.这就是我喜欢 python3 的原因:
实际上这很丑陋:
That's why I love python3:
actually this is ugly: