[英]How Do I Print things on the same line from two print statements which are in two for loops
[英]How can I show the output of two print statements on the same line?
我有两个单独的打印声明:
打印“123”
打印“456”
如何使这两个打印语句出现在同一行? 注意我需要使用2个打印语句
输出:
123456
在python 1.x和2.x中,尾随逗号将执行您想要的操作(其他人提到有关插入的额外空间的警告):
print "123",
print "456"
在python 3.x中 - 或者在python 2.6-2.7中使用from __future__ import print_function
- print
是一个函数,你应该使用end=""
强制没有额外的结束字符:
print("123", end="")
print("456")
您正在打印的字符串后面的逗号将禁止换行。 \\b
是表示ASCII退格的特殊字符。
print '123',
print '\b456'
print "123",
print "456"
或者,您可以使用sys.stdout.write
:
sys.stdout.write('123')
sys.stdout.write('456\n')
print '123',
print '\b456'
print "123",
print "456"
适用于Python 3+
print("123", end="")
print("456")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.