简体   繁体   English

根据用户输入创建名称列表()

[英]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.我一直在尝试找到一种方法将用户的姓名输入转换为 python 列表,以便稍后处理这些信息。 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.当我编写下面的代码时, print() output 只是名字的第二个字母,而我实际上打算将它作为列表中的第二个名字。 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])

Use split() to turn a string into a list of words:使用split()将字符串转换为单词列表:

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 (eg split(",") to split on commas).默认情况下split()在空格上拆分,但它需要一个可选参数,允许您在其他字符串上拆分(例如split(",")在逗号上拆分)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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