Skip to main content
 首页 » 编程设计

python之在一行 Python 3.2.2 中打印一个 for 循环

2025年05月04日13傻小

我正在尝试打印一行代码,但它有很多,我认为如果我将它们全部打印在一行上,它看起来会更整洁。 我正在尝试使用 for 循环打印一个列表,我想在同一行上打印所有内容。

    for i in ALLROOMS: 
            print(i.name) 

请您参考如下方法:

使用 end="":

print (i.name, end="")

例子:

In [2]: for i in range(5): 
   ...:     print(i, end=" ") 
   ...:      
0 1 2 3 4  

关于 print() 的帮助:

print(value, ..., sep=' ', end='\n', file=sys.stdout) 
 
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.