打印功能中的 sep 和 end 有什么区别? [英] What are the difference between sep and end in print function?
问题描述
pets = ['boa', 'cat', 'dog']对于宠物中的宠物:打印(宠物)蟒蛇猫狗>>>对于宠物中的宠物:打印(宠物,结束=",)蟒蛇,猫,狗,>>>对于宠物中的宠物:打印(宠物,结束 ='!!!')蟒蛇!!!猫!!!狗!!!
但是 sep 呢?我试图用 sep 替换 end 但什么也没发生,但我知道 sep 用于在打印时进行分离,我如何以及何时使用 sep?sep 和 end 有什么区别?
printfunction 使用 sep
分隔参数,并在最后一个参数之后使用 end
.你的例子令人困惑,因为你只给了它一个论点.这个例子可能更清楚:
当然,sep
和 end
只适用于 Python 3 的打印功能.对于 Python 2,以下内容是等效的.
您还可以在 Python 2 中使用向后移植的打印函数版本:
<预><代码>>>>从 __future__ 导入 print_function>>>print('boa', 'cat', 'dog', sep=', ', end='!!!\n')蟒蛇,猫,狗!!!pets = ['boa', 'cat', 'dog']
for pet in pets:
print(pet)
boa
cat
dog
>>> for pet in pets:
print(pet, end=', ')
boa, cat, dog,
>>> for pet in pets:
print(pet, end='!!! ')
boa!!! cat!!! dog!!!
but what about sep? i tried to replace end by sep but nothing happened but i know that sep is used to separete while printing, how and when can i use sep? what are the differences between sep and end?
The print function uses sep
to separate the arguments, and end
after the last argument. Your example was confusing because you only gave it one argument. This example might be clearer:
>>> print('boa', 'cat', 'dog', sep=', ', end='!!!\n')
boa, cat, dog!!!
Of course, sep
and end
only work in Python 3's print function. For Python 2, the following is equivalent.
>>> print ', '.join(['boa', 'cat', 'dog']) + '!!!'
boa, cat, dog!!!
You can also use a backported version of the print function in Python 2:
>>> from __future__ import print_function
>>> print('boa', 'cat', 'dog', sep=', ', end='!!!\n')
boa, cat, dog!!!
这篇关于打印功能中的 sep 和 end 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!