Home > front end >  How to print a worded list line by line
How to print a worded list line by line

Time:05-14

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

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)

CodePudding user response:

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

CodePudding user response:

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)
  • Related