[英]How to print a worded list line by line
只是想知道如何打印一个包含名称的列表变量,逐行逐个名称,以便每行一个名称,例如:
卢卡
安德鲁
周杰伦
萝拉
我正在使用的代码:
IndNames=input("""Please enter the names of the 20 individual competitors in this format;
'name,name,name,name,name' etc until you have entered 20 names:
""")
print("Congratulations, here are the individual competitors: " + IndNames)
如果你想做实验,这里有一些我一直在使用的名字的测试数据:Luka,Ted,Arselan,Jack,Wez,Isaac,Neil,Yew,Ian,John,Bruce,Kent,Clark,Dianna,Selena ,Gamora,Nebula,Mark,Steven,Gal(感谢所有帮助,我是一名大学生,对 Python 还很陌生)
为此,您可以使用按“,”分割并使用“\n”方法加入:
print("Congratulations, here are the individual competitors: \n" + "\n".join(IndNames.split(',')))
添加一些细节:
splitted_list = IndNames.split(",")
使用逗号作为分隔符拆分字符串,因此您将得到:
["Name1", "Name2", "NameN"]
然后你需要将它加入回带有换行符的字符串。
"\n".join(splitted_list)
将导致:
"Name1\nName2\nNameN"
打印为
Name1
Name2
NameN
只需将用户根据逗号分隔符给出的输入拆分为一个列表,然后使用循环在单独的行上打印出每个名称,如下所示
IndNames=input("""Please enter the names of the 20 individual competitors in this format;
'name,name,name,name,name' etc until you have entered 20 names:
""")
# split the input using the comma delimter
names=IndNames.split(",")
for name in names:
print(name)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.