
[英]how can i print multiple instances from a file in a single line using python
[英]How can I print list of items from multiple lines into a single line in Python?
我正在尝试将项目列表从多行转换为单行字符串,反之亦然。 我可以使用以下代码将项目从单行字符串转换为多行垂直项目列表:
Items = ["Apple","Ball","Cat"]
A = ("\n".join(Items))
print (A)
其中打印项目列表如下:
Apple
Ball
Cat
我都设置了上面的代码
我的问题是关于下面的第二个代码(我无法弄清楚):
但是,我无法将垂直列表从多行转换为单行字符串。
我在多行中有以下项目;
Apple
Ball
Cat
我想将它们打印在一行中:
Apple Ball Cat
我感谢任何形式的建议。
用这个替换你的代码:
A = (" ".join(Items))
您正在使用换行符'\\n'
加入项目。 尝试只用一个空格' '
加入它们。
所以代替
Items = ["Apple","Ball","Cat"]
A = ("\n".join(Items))
print (A)
你做
Items = ["Apple","Ball","Cat"]
A = (" ".join(Items))
print (A)
不需要join()
things = ["Apple","Ball","Cat"]
s1 = ''
for item in things:
s1 += item + ' '
print(s1.rstrip())
# output
''' Apple Ball Cat '''
print(A,end="") 用这个替换最后一行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.