'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, 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)
Solution 1:[1]
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
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | TechGeek |