简体   繁体   中英

How to print a worded list line by line

Just wondering how I can print a list variable containing names, name by name line by line so its one name per line, eg:

Luka

Andrew

Jay

Lola

The code I'm usinng:

    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)

here is some test data for the names I've been using if you'd like to experiment: Luka,Ted,Arselan,Jack,Wez,Isaac,Neil,Yew,Ian,John,Bruce,Kent,Clark,Dianna,Selena,Gamora,Nebula,Mark,Steven,Gal (I appreciate any and all help, I'm a college student and fairly new to Python)

You can use split by "," and join with "\n" methods for this:

print("Congratulations, here are the individual competitors: \n" + "\n".join(IndNames.split(',')))

Adding some details:

splitted_list = IndNames.split(",")

Splits your string using comma as separator, so you will get this:

["Name1", "Name2", "NameN"]

Then you need to join it back to string with newline character.

"\n".join(splitted_list)

will result into:

"Name1\nName2\nNameN"

which is printed as

Name1
Name2
NameN

Just split the input given by the user based on the comma delimiter into a list and then use a loop to print out each name on a separate line like below

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM