Skip to main content
 首页 » 编程设计

python之意外的打印行为 Python 3

2025年05月04日9Leo_wl

我正在运行 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')