python2和python3 print輸出不換行


python2 print不換行

在print最后加上一個逗號,會把兩個輸出打印在同一行,不過兩個輸出之間有一個空格的間隔,例如:

print '{0}'.format(123),
print '{0}'.format(456)

輸出:

123 456

如果沒有逗號:

print '{0}'.format(123)
print '{0}'.format(456)

輸出:

123
456

python3 print不換行

python3中print函數中的參數end默認值為'\n',表示換行,給end賦值為空,就不會換行了,例如:

print (123,end='')
print (456,end='')

輸出:

123456


python2 print不換行另一種實現

python2在文件首行加上 from __future__ import print_function ,也可以使用python3中給end參數賦空(值)的方式實現輸出不換行:

from __future__ import print_function
print (123,end='')
print (456,end='')

輸出:
123456

【轉】https://blog.csdn.net/dcrmg/article/details/79091926

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM