-1

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)

luka
  • 1
  • 2
  • Does this answer your question? [Printing list elements on separate lines in Python](https://stackoverflow.com/questions/6167731/printing-list-elements-on-separate-lines-in-python) – vhaska May 13 '22 at 10:33

2 Answers2

0

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
pL3b
  • 1,155
  • 1
  • 3
  • 18
  • I have added some details. Also you can find some info about .join method here: https://www.w3schools.com/python/ref_string_join.asp and about split - here: https://www.w3schools.com/python/ref_string_split.asp – pL3b May 13 '22 at 10:37
0

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)
TechGeek
  • 316
  • 1
  • 11