如何将用户输入分配给变量或列表
假设我有一行: a = input("输入你的姓名和年龄:")
如何使变量“a”存储输入的每个部分,例如将“George 25”存储为“George,25”作为标准变量或列表?我想要做的就是能够将用户输入存储为列表或变量中的单独元素,以便用空格分隔的单词输入将成为变量中的单独元素。这够清楚了吗?我正在使用Python 3.0
Let's say that I have a line with: a = input("Enter your name and age:")
How can I make the variable "a" store each section of the input, lets say "George 25" as "George, 25" either as a standard variable or a list? All I want to do is be able to store user input as separate elements in a list or variable so that input with words separated by a space would become separate elements in a variable. Is this clear enough? I am using Python 3.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用 str.split():
这会创建一个 输入中每个空格分隔的单词的列表。
然后您可以访问每个部分:
这就是您想要的吗?
How about using str.split():
This creates a list of each space-separated word in the input.
Then you can access each part as:
Is that what you're after ?