Search code examples
pythonlistuser-input

Creating a list of names base on user input()


I've been trying to find a way to transform users' name inputs into a python list, so I could later process the information. However, since I'm not very experienced, I can't find a way to do such a thing. When I write the code below, the print() output, is only the second letter of the first name, while I actually intend it to be the second name on the list. So, my question is, how can I make it so that each different name is a different element in the list?

# Creates a list of the players
players = input("Insert players\n")
list(players)
print(players[1])

Solution

  • Use split() to turn a string into a list of words:

    players = input("Insert players\n").split()
    print(players[1])  # prints the second player name in the list
    

    By default split() splits on whitespace, but it takes an optional argument that allows you to split on other strings (e.g. split(",") to split on commas).