[英]separate output of input() function in Python [duplicate]
这个问题已经在这里有了答案:
有没有办法在Python中将输出与输入函数分开?
例如,假设我们要插入数字和名称。
input('Give number and name:')
Give number and name:14,John
'14,John'
我们得到“ '14,John'
。 有没有办法采取'14','John'
?
提前致谢。
这样行吗?
user_input = input("Please write a number then a name, separated by a comma: ")
number, name = user_input.split(", ")
使用.split()
>>> input('Give number and name: ').split(',')
Give number and name: 14,John
['14','John']
要么
>>> number, name = input('Give number and name: ').split(',')
Give number and name: 14,John
>>> number
'14'
>>> name
'John'
请注意,它们都是strings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.