![](/img/trans.png)
[英]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.