[英]Unexpected print behaviour Python 3
我正在閱讀有關python 3的教程,並且在打印時遇到了奇怪的現象。 例如:
print ("\nUnpickling lists.")
pickle_file = open("pickles1.dat", "rb")
variety = pickle.load(pickle_file)
shape = pickle.load(pickle_file)
brand = pickle.load(pickle_file)
print (variety,"\n",shape,"\n",brand)
pickle_file.close()
給我:
Unpickling lists.
['sweet', 'hot', 'dill']
['whole', 'spear', 'chip']
['Claussen', 'Heinz', 'Vlassic']
如何避免第二個和第三個列表的打印輸出行開頭出現多余的空間?
只需將'\\n'
指定為分隔符,即可避免在每個項目之間添加換行符:
print(variety, shape, brand, sep='\n')
使用sep = ''
:
print (variety,"\n",shape,"\n",brand, sep = '')
sep
默認值為一個空格:
>>> print('a','b')
a b
>>> print('a','b', sep ='')
ab
print
幫助: print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file: a file-like object (stream); defaults to the current sys.stdout.
sep: string inserted between values, default a space. <-----
end: string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.