简体   繁体   English

如何在 python 中使用空格分隔的输入

[英]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.

相关问题 如何在Python 3中获取由空格和回车分隔的多组输入? - How to take multiple sets of inputs separated by space and carriage return in Python 3? "在python中读取空格分隔的输入" - Reading Space separated input in python 在Python中获取空格分隔的输入 - getting space separated input in python 如何在字典中使用空格分隔的输入,并将每个新输入与新键值存储在一起? - How to take space separated input in dictionary with every new input stored against a new key value? 如何使用Python 3中的readlines读取由空格分隔的整数输入文件? - How to read an input file of integers separated by a space using readlines in Python 3? 如何在 python 的数组中输入一个空格分隔的整数数组 append? - How to append an array of space-separated integers input in an array in python? 以空格分隔的输入具有不同的数据类型 - Take space separated input having different data types 使用python循环获取空格分隔的输入 - Taking space separated input in loop with python 将 integer 带空格输入,将值存入 python - Take integer Input with space and store the value in python 如何检查整个输入字符串(用空格分隔的实数)是否与 Python 中的正则表达式匹配? - How to check if the whole input string (real numbers separated by a space) matches a regex in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM