[英]How to take space separated input in python
I want to take the following in single line from user我想从用户那里单行获取以下内容
Abc 0 1 0
How can this be done?如何才能做到这一点? I tried
我试过
map(int,input().split())
input() and then split
input().split(" ")
I am getting the following error:我收到以下错误:
File "main.py", line 11, in <module>
name = input()
File "<string>", line 1
Abc 0 0 2
^
SyntaxError: invalid syntax
but it is not giving the desired result.但它没有给出预期的结果。
#a = input()
a = "Abc 0 1 0"
inp_list = a.split()
print(inp_list)
input()
returns a list of strings. input()
返回一个字符串列表。 You have to handle it as such你必须这样处理
for e in inp_list:
try:
e_int = int(e)
except ValueError:
print(e)
continue
print(e_int)
Try this:试试这个:
inp1, inp2, inp3, inp4 = input().split(“ “)
You could directly unpack them in a function like that:您可以像这样直接将它们解压缩到 function 中:
func(*input().split(“ “))
text = input("Say something")
print(text.split())
print(text.replace(" ", ""))
output output
Say somethingthis has spaces but it wont when it is done
['this', 'has', 'spaces', 'but', 'it', 'wont', 'when', 'it', 'is', 'done']
thishasspacesbutitwontwhenitisdone
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.