简体   繁体   English

打印函数中“结束”参数的作用是什么?

[英]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.

相关问题 如何在打印 function 中使用“end”关键字参数? - How to use 'end' keyword argument with print function? 打印功能中的 sep 和 end 有什么区别? - What are the difference between sep and end in print function? 误解 end= 参数如何在 print() function 在 while 循环中工作 - Misunderstanding how end= argument works in print() function in a while loop 为什么使用print函数的end参数改变输出时间? - Why usage of end argument of print function changing the output time? 将结束参数与打印函数一起使用时的 Python SyntaxError - Python SyntaxError when using end argument with print function 不能在 Print() function 的 Else 语句中使用 End 参数 - Cannot use End argument within Else statement in Print() function 返回结果并在函数结束时打印它之间的最佳效果是什么? - What's the best between return the result and print it at the end of the function? print() 中的结束参数 function - End parameter in print() function Python:如何使print函数中的'end'关键字参数停止运行以实现此目的 - Python : How to make the 'end' keyword argument in print function to stop functioning for this purpose 为什么在 Python 的打印函数中传递给关键字参数 end 的参数没有在下面的上下文中按预期工作? - Why does not the parameter passed to the keyword argument end in the print function of Python does not work as expected in the below context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM