[英]What is the job of the "end" argument in the print function?
for example in this code below the with the end the integers stay on the same line but without the it does not.例如,在下面的代码中,整数保持在同一行,但没有它就不会。
num = 5
for i in range(1, num +1):
for j in range(num, i-1, -1):
print(j, end="")
print()
The end
statement for printing in Python allows the programmer to define a custom ending character for each print
call other than the default \\n
Python 中用于打印的
end
语句允许程序员为每个print
调用定义一个自定义的结束字符,而不是默认的\\n
For instance, if you have a function that is to print all values within a list on the same line, you do:例如,如果您有一个函数要在同一行上打印列表中的所有值,您可以执行以下操作:
def value(l):
for items in l:
print(l, end=' ')
So if the list argued to this function contains the values [1, 2, 3, 4]
, it will print them in this manner: 1 2 3 4
.因此,如果与此函数争论的列表包含值
[1, 2, 3, 4]
,它将以这种方式打印它们: 1 2 3 4
。 If this new ending character parameter was not defined they would be printed:如果没有定义这个新的结束字符参数,它们将被打印出来:
1
2
3
4
The same principle applies for ANY value you provide for the end
option.同样的原则适用于您为
end
选项提供的任何值。
In Python 3, the default ist end='\\n'
for the print function which is a newline after the string is printed.在 Python 3 中,打印函数的默认 ist
end='\\n'
是打印字符串后的换行符。
To suppress this newline, one can set end=''
so that the next print starts in the same line.要取消此换行符,可以设置
end=''
以便下一个打印在同一行开始。
This is unrelated to the for
loop.这与
for
循环无关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.