[英]Suppress print newline in python 3 str.format
我正在使用Python 3,並嘗試將我的print語句與str.format一起使用。
例如:
print ('{0:3d} {1:6d} {2:10s} '.format (count1,count2,string1))
當我嘗試使用end=''
來抑制后續換行符時,會忽略它。 換行總是會發生。
如何抑制后續換行符?
資源:
int1= 1
int2 = 999
string1 = 'qwerty'
print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1))
print ('newline')
print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1,end=''))
print ('newline')
Python 3.4.0 (default, Apr 11 2014, 13:05:11)
[GCC 4.8.2] on linux
Type "copyright", "credits" or "license()" for more information.
1 999 qwerty
新隊1 999 qwerty
新隊
您的問題是您將end=''
參數傳遞給format
函數,而不是print
函數。
改變這一行:
print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1,end=''))
對此:
print ( '{0:3d} {1:6d} {2:10s} '.format (int1,int2,string1), end='')
順便說一句,你也應該給PEP8一個讀數。 它定義了Python編碼樣式的標准,您應該嘗試遵循這些標准,除非您與一組已經就其他樣式標准達成一致的人合作。 特別是,你的間距在函數調用周圍有點奇怪 - 你不應該在函數名和參數括號之間,或者在括號和第一個參數之間有空格。 我以保持當前風格的方式編寫了我建議的解決方案,但它看起來應該更像這樣:
print('{0:3d} {1:6d} {2:10s} '.format(int1, int2, string1), end='')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.