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