Python 将输入分成两部分。蟒蛇 3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19063114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split an input into two. Python 3
提问by dkentre
I've used this method before but can't find it in any of my codes so here I am on Stack Overflow :) What I'm trying to do is split an input into two( the user is asked to enter two digits separated by space). How would you call the first digit a and the second digit b ? The code so far doesn't seem to work.
我以前使用过这种方法,但在我的任何代码中都找不到它,所以我在这里使用 Stack Overflow :) 我想要做的是将输入分成两部分(要求用户输入分隔开的两位数)按空间)。你如何称呼第一个数字 a 和第二个数字 b ?到目前为止的代码似乎不起作用。
a,b= input(split" "("Please enter two digits separated by space"))
回答by TerryA
The str.split()
function is an attributeof the type str(string). To call it, you do:
该str.split()
函数是一个str(字符串)类型的属性。要调用它,您可以:
input("Please enter two digits separated by space").split()
Note that .split(" ")
isn't needed as that is what it is by default.
请注意,这.split(" ")
是不需要的,因为默认情况下是这样。
回答by Games Brainiac
You're calling the function wrongly.
您错误地调用了该函数。
>>> "hello world".split()
['hello', 'world']
split
slits a string by a space by default, but you can change this behavior:
split
默认情况下按空格分割字符串,但您可以更改此行为:
>>> "hello, world".split(',')
['hello', ' world']
In your case:
在你的情况下:
a,b= input("Please enter two digits separated by space").split()