Printing each item of a variable on a separate line in Python(在 Python 中将变量的每一项打印在单独的行上)
问题描述
我正在尝试在 Python 中打印一个包含数字的列表,并且当它打印列表中的项目时,所有项目都打印在同一行.
I am trying to print a list in Python that contains digits and when it prints the items in the list all print on the same line.
print ("{} ".format(ports))
这是我的输出
[60, 89, 200]
我怎样才能看到这种形式的结果:
how can I see the result in this form:
60
89
200
我试过 print ("
".join(ports))
但这不起作用.
I have tried print ("
".join(ports))
but that does not work.
推荐答案
遍历列表并在新行打印每个项目:
Loop over the list and print each item on a new line:
for port in ports:
print(port)
或在加入之前将整数转换为字符串:
or convert your integers to strings before joining:
print('
'.join(map(str, ports)))
或告诉 print()
使用换行符作为分隔符,并使用 *
splat 语法将列表作为单独的参数传递:
or tell print()
to use newlines as separators and pass in the list as separate arguments with the *
splat syntax:
print(*ports, sep='
')
演示:
>>> ports = [60, 89, 200]
>>> for port in ports:
... print(port)
...
60
89
200
>>> print('
'.join(map(str, ports)))
60
89
200
>>> print(*ports, sep='
')
60
89
200
这篇关于在 Python 中将变量的每一项打印在单独的行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Python 中将变量的每一项打印在单独的行上


基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01