Two values from one input in python?
This is somewhat of a simple question and I hate to ask it here, but I can't seem the find the answer anywhere else: is it possible to get multiple values from the user in one line of Python?
For instance, in C I can do something like this: scanf("%d %d", &var1, &var2)
. However, I can't figure out what the Python equivalent of that is. I figured it would just be something like var1, var2 = input("Enter two numbers here: ")
, but that doesn't work and I'm not complaining because it wouldn't make a whole lot of sense if it did.
Does anyone out there know a good way to do this elegantly and concisely?
python variables input
add a comment |
This is somewhat of a simple question and I hate to ask it here, but I can't seem the find the answer anywhere else: is it possible to get multiple values from the user in one line of Python?
For instance, in C I can do something like this: scanf("%d %d", &var1, &var2)
. However, I can't figure out what the Python equivalent of that is. I figured it would just be something like var1, var2 = input("Enter two numbers here: ")
, but that doesn't work and I'm not complaining because it wouldn't make a whole lot of sense if it did.
Does anyone out there know a good way to do this elegantly and concisely?
python variables input
Very similar recent question: stackoverflow.com/questions/959412/parsing-numbers-in-python
– Nicolas Dumazet
Jun 7 '09 at 5:51
add a comment |
This is somewhat of a simple question and I hate to ask it here, but I can't seem the find the answer anywhere else: is it possible to get multiple values from the user in one line of Python?
For instance, in C I can do something like this: scanf("%d %d", &var1, &var2)
. However, I can't figure out what the Python equivalent of that is. I figured it would just be something like var1, var2 = input("Enter two numbers here: ")
, but that doesn't work and I'm not complaining because it wouldn't make a whole lot of sense if it did.
Does anyone out there know a good way to do this elegantly and concisely?
python variables input
This is somewhat of a simple question and I hate to ask it here, but I can't seem the find the answer anywhere else: is it possible to get multiple values from the user in one line of Python?
For instance, in C I can do something like this: scanf("%d %d", &var1, &var2)
. However, I can't figure out what the Python equivalent of that is. I figured it would just be something like var1, var2 = input("Enter two numbers here: ")
, but that doesn't work and I'm not complaining because it wouldn't make a whole lot of sense if it did.
Does anyone out there know a good way to do this elegantly and concisely?
python variables input
python variables input
edited Oct 23 '11 at 18:21
Jonta
241524
241524
asked Jun 7 '09 at 5:27
Donovan
Very similar recent question: stackoverflow.com/questions/959412/parsing-numbers-in-python
– Nicolas Dumazet
Jun 7 '09 at 5:51
add a comment |
Very similar recent question: stackoverflow.com/questions/959412/parsing-numbers-in-python
– Nicolas Dumazet
Jun 7 '09 at 5:51
Very similar recent question: stackoverflow.com/questions/959412/parsing-numbers-in-python
– Nicolas Dumazet
Jun 7 '09 at 5:51
Very similar recent question: stackoverflow.com/questions/959412/parsing-numbers-in-python
– Nicolas Dumazet
Jun 7 '09 at 5:51
add a comment |
18 Answers
18
active
oldest
votes
The Python way to map
printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)
would be
var1, var2 = raw_input("Enter two numbers here: ").split()
Note that we don't have to explicitly specify split(' ')
because split()
uses any whitespace characters as delimiter as default. That means if we simply called split()
then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,
Python has dynamic typing so there is no need to specify %d
. However, if you ran the above then var1
and var2
would be both Strings. You can convert them to int
using another line
var1, var2 = [int(var1), int(var2)]
Or you could use list comprehension
var1, var2 = [int(x) for x in [var1, var2]]
To sum it up, you could have done the whole thing with this one-liner:
# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]
# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
add a comment |
You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):
raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'
So you could rewrite your try to:
var1, var2 = raw_input("enter two numbers:").split(' ')
Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).
Also be aware that var1 and var2 will still be strings with this method when not cast to int.
add a comment |
In Python 2.*
, input
lets the user enter any expression, e.g. a tuple:
>>> a, b = input('Two numbers please (with a comma in between): ')
Two numbers please (with a comma in between): 23, 45
>>> print a, b
23 45
In Python 3.*
, input
is like 2.*
's raw_input
, returning you a string that's just what the user typed (rather than eval
ing it as 2.*
used to do on input
), so you'll have to .split
, and/or eval
, &c but you'll also be MUCH more in control of the whole thing.
Oh noes. Please never do this:input()
allows for arbitrary code execution. Just typeexit('pwned!')
at the input prompt and see what happens.
– 9000
Mar 21 '17 at 20:18
@9000 I got aValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries usinginput()
now.
– unclexo
Jul 22 '18 at 6:44
add a comment |
If you need to take two integers say a,b in python you can use map function.
Suppose input is,
1
5 3
1 2 3 4 5
where 1 represent test case, 5 represent number of values and 3 represents a task value and in next line given 5 values, we can take such input using this method in PYTH 2.x Version.
testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())
You can replace 'int' in map() with another datatype needed.
add a comment |
All input will be through a string. It's up to you to process that string after you've received it. Unless that is, you use the eval(input()) method, but that isn't recommended for most situations anyway.
input_string = raw_input("Enter 2 numbers here: ")
a, b = split_string_into_numbers(input_string)
do_stuff(a, b)
add a comment |
You have to use the split()
method which splits the input into two different inputs. Whatever you pass into the split is looked for and the input is split from there. In most cases its the white space.
For example, You give the input 23 24 25
. You expect 3 different inputs like
num1 = 23
num2 = 24
num3 = 25
So in Python, You can do
num1,num2,num3 = input().split(" ")
add a comment |
Check this handy function:
def gets(*types):
return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])
# usage:
a, b, c = gets(int, float, str)
1
Index access is not needed!return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.
– 9000
Mar 21 '17 at 20:22
add a comment |
The solution I found is the following:
Ask the user to enter two numbers separated by a comma or other character
value = input("Enter 2 numbers (separated by a comma): ")
Then, the string is split: n
takes the first value and m
the second one
n,m = value.split(',')
Finally, to use them as integers, it is necessary to convert them
n, m = int(n), int(m)
add a comment |
The easiest way that I found for myself was using split function with input
Like you have two variable a,b
a,b=input("Enter two numbers").split()
That's it.
there is one more method(explicit method)
Eg- you want to take input in three values
value=input("Enter the line")
a,b,c=value.split()
EASY..
add a comment |
This is a sample code to take two inputs seperated by split command and delimiter as ","
>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'
Other variations of delimiters that can be used are as below :var1, var2 = input("enter two numbers:").split(',')
var1, var2 = input("enter two numbers:").split(';')
var1, var2 = input("enter two numbers:").split('/')
var1, var2 = input("enter two numbers:").split(' ')
var1, var2 = input("enter two numbers:").split('~')
add a comment |
For n
number of inputs declare the variable as an empty list and use the same syntax to proceed:
>>> x=input('Enter value of a and b').split(",")
Enter value of a and b
1,2,3,4
>>> x
['1', '2', '3', '4']
add a comment |
a,b=[int(x) for x in input().split()]
print(a,b)
Output
10 8
10 8
add a comment |
In Python 3, raw_input()
was renamed to input()
.
input([prompt])
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised.
So you can do this way
x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))
If you do not put two numbers using a space you would get a ValueError
exception. So that is good to go.
N.B. If you need old behavior of input()
, use eval(input())
add a comment |
I tried this in Python 3 , seems to work fine .
a, b = map(int,input().split())
print(a)
print(b)
Input : 3 44
Output :
3
44
add a comment |
Two inputs separated by space:
x,y=input().split()
add a comment |
This solution is being used for converting multiple string like ("22 44 112 2 34") to an integer list and you can access to the values in a list.
n = input("") # Like : "22 343 455 54445 22332"
if n[:-1] != " ":
n += " "
char = ""
list =
for i in n :
if i != " ":
char += i
elif i == " ":
list.append(int(char))
char = ""
print(list) # Be happy :))))
add a comment |
You can do this way
a, b = map(int, input().split())
OR
a, b = input().split()
print(int(a))
OR
MyList = list(map(int, input().split()))
a = MyList[0]
b = MyList[1]
add a comment |
if we want to two inputs in a single line so, the code is are as follows enter code here
x,y=input().split(" ")
print(x,y)
after giving value as input we have to give spaces between them because of split(" ") function or method.
but these values are of string type if to perform some arithmetic operations we have to convert the type of x,y are as follows
int(x),int(y)
we can do this also with the help of list in python.enter code here
list1=list(map(int,input().split()))
print(list1)
this list gives the elements in int type
New contributor
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f961263%2ftwo-values-from-one-input-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
The Python way to map
printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)
would be
var1, var2 = raw_input("Enter two numbers here: ").split()
Note that we don't have to explicitly specify split(' ')
because split()
uses any whitespace characters as delimiter as default. That means if we simply called split()
then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,
Python has dynamic typing so there is no need to specify %d
. However, if you ran the above then var1
and var2
would be both Strings. You can convert them to int
using another line
var1, var2 = [int(var1), int(var2)]
Or you could use list comprehension
var1, var2 = [int(x) for x in [var1, var2]]
To sum it up, you could have done the whole thing with this one-liner:
# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]
# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
add a comment |
The Python way to map
printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)
would be
var1, var2 = raw_input("Enter two numbers here: ").split()
Note that we don't have to explicitly specify split(' ')
because split()
uses any whitespace characters as delimiter as default. That means if we simply called split()
then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,
Python has dynamic typing so there is no need to specify %d
. However, if you ran the above then var1
and var2
would be both Strings. You can convert them to int
using another line
var1, var2 = [int(var1), int(var2)]
Or you could use list comprehension
var1, var2 = [int(x) for x in [var1, var2]]
To sum it up, you could have done the whole thing with this one-liner:
# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]
# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
add a comment |
The Python way to map
printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)
would be
var1, var2 = raw_input("Enter two numbers here: ").split()
Note that we don't have to explicitly specify split(' ')
because split()
uses any whitespace characters as delimiter as default. That means if we simply called split()
then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,
Python has dynamic typing so there is no need to specify %d
. However, if you ran the above then var1
and var2
would be both Strings. You can convert them to int
using another line
var1, var2 = [int(var1), int(var2)]
Or you could use list comprehension
var1, var2 = [int(x) for x in [var1, var2]]
To sum it up, you could have done the whole thing with this one-liner:
# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]
# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
The Python way to map
printf("Enter two numbers here: ");
scanf("%d %d", &var1, &var2)
would be
var1, var2 = raw_input("Enter two numbers here: ").split()
Note that we don't have to explicitly specify split(' ')
because split()
uses any whitespace characters as delimiter as default. That means if we simply called split()
then the user could have separated the numbers using tabs, if he really wanted, and also spaces.,
Python has dynamic typing so there is no need to specify %d
. However, if you ran the above then var1
and var2
would be both Strings. You can convert them to int
using another line
var1, var2 = [int(var1), int(var2)]
Or you could use list comprehension
var1, var2 = [int(x) for x in [var1, var2]]
To sum it up, you could have done the whole thing with this one-liner:
# Python 3
var1, var2 = [int(x) for x in input("Enter two numbers here: ").split()]
# Python 2
var1, var2 = [int(x) for x in raw_input("Enter two numbers here: ").split()]
edited Jan 23 '18 at 2:06
answered Jun 7 '09 at 5:59
kizzx2kizzx2
11k136476
11k136476
add a comment |
add a comment |
You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):
raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'
So you could rewrite your try to:
var1, var2 = raw_input("enter two numbers:").split(' ')
Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).
Also be aware that var1 and var2 will still be strings with this method when not cast to int.
add a comment |
You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):
raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'
So you could rewrite your try to:
var1, var2 = raw_input("enter two numbers:").split(' ')
Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).
Also be aware that var1 and var2 will still be strings with this method when not cast to int.
add a comment |
You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):
raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'
So you could rewrite your try to:
var1, var2 = raw_input("enter two numbers:").split(' ')
Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).
Also be aware that var1 and var2 will still be strings with this method when not cast to int.
You can't really do it the C way (I think) but a pythonic way of doing this would be (if your 'inputs' have spaces in between them):
raw_answer = raw_input()
answers = raw_answer.split(' ') # list of 'answers'
So you could rewrite your try to:
var1, var2 = raw_input("enter two numbers:").split(' ')
Note that this it somewhat less flexible than using the 'first' solution (for example if you add a space at the end this will already break).
Also be aware that var1 and var2 will still be strings with this method when not cast to int.
edited Jun 7 '09 at 5:45
answered Jun 7 '09 at 5:39
ChristopheDChristopheD
80.4k21143165
80.4k21143165
add a comment |
add a comment |
In Python 2.*
, input
lets the user enter any expression, e.g. a tuple:
>>> a, b = input('Two numbers please (with a comma in between): ')
Two numbers please (with a comma in between): 23, 45
>>> print a, b
23 45
In Python 3.*
, input
is like 2.*
's raw_input
, returning you a string that's just what the user typed (rather than eval
ing it as 2.*
used to do on input
), so you'll have to .split
, and/or eval
, &c but you'll also be MUCH more in control of the whole thing.
Oh noes. Please never do this:input()
allows for arbitrary code execution. Just typeexit('pwned!')
at the input prompt and see what happens.
– 9000
Mar 21 '17 at 20:18
@9000 I got aValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries usinginput()
now.
– unclexo
Jul 22 '18 at 6:44
add a comment |
In Python 2.*
, input
lets the user enter any expression, e.g. a tuple:
>>> a, b = input('Two numbers please (with a comma in between): ')
Two numbers please (with a comma in between): 23, 45
>>> print a, b
23 45
In Python 3.*
, input
is like 2.*
's raw_input
, returning you a string that's just what the user typed (rather than eval
ing it as 2.*
used to do on input
), so you'll have to .split
, and/or eval
, &c but you'll also be MUCH more in control of the whole thing.
Oh noes. Please never do this:input()
allows for arbitrary code execution. Just typeexit('pwned!')
at the input prompt and see what happens.
– 9000
Mar 21 '17 at 20:18
@9000 I got aValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries usinginput()
now.
– unclexo
Jul 22 '18 at 6:44
add a comment |
In Python 2.*
, input
lets the user enter any expression, e.g. a tuple:
>>> a, b = input('Two numbers please (with a comma in between): ')
Two numbers please (with a comma in between): 23, 45
>>> print a, b
23 45
In Python 3.*
, input
is like 2.*
's raw_input
, returning you a string that's just what the user typed (rather than eval
ing it as 2.*
used to do on input
), so you'll have to .split
, and/or eval
, &c but you'll also be MUCH more in control of the whole thing.
In Python 2.*
, input
lets the user enter any expression, e.g. a tuple:
>>> a, b = input('Two numbers please (with a comma in between): ')
Two numbers please (with a comma in between): 23, 45
>>> print a, b
23 45
In Python 3.*
, input
is like 2.*
's raw_input
, returning you a string that's just what the user typed (rather than eval
ing it as 2.*
used to do on input
), so you'll have to .split
, and/or eval
, &c but you'll also be MUCH more in control of the whole thing.
answered Jun 7 '09 at 6:19
Alex MartelliAlex Martelli
627k12810401280
627k12810401280
Oh noes. Please never do this:input()
allows for arbitrary code execution. Just typeexit('pwned!')
at the input prompt and see what happens.
– 9000
Mar 21 '17 at 20:18
@9000 I got aValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries usinginput()
now.
– unclexo
Jul 22 '18 at 6:44
add a comment |
Oh noes. Please never do this:input()
allows for arbitrary code execution. Just typeexit('pwned!')
at the input prompt and see what happens.
– 9000
Mar 21 '17 at 20:18
@9000 I got aValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries usinginput()
now.
– unclexo
Jul 22 '18 at 6:44
Oh noes. Please never do this:
input()
allows for arbitrary code execution. Just type exit('pwned!')
at the input prompt and see what happens.– 9000
Mar 21 '17 at 20:18
Oh noes. Please never do this:
input()
allows for arbitrary code execution. Just type exit('pwned!')
at the input prompt and see what happens.– 9000
Mar 21 '17 at 20:18
@9000 I got a
ValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries using input()
now.– unclexo
Jul 22 '18 at 6:44
@9000 I got a
ValueError
exception. That's perfect. So it's not that something harmful you meant. So no worries using input()
now.– unclexo
Jul 22 '18 at 6:44
add a comment |
If you need to take two integers say a,b in python you can use map function.
Suppose input is,
1
5 3
1 2 3 4 5
where 1 represent test case, 5 represent number of values and 3 represents a task value and in next line given 5 values, we can take such input using this method in PYTH 2.x Version.
testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())
You can replace 'int' in map() with another datatype needed.
add a comment |
If you need to take two integers say a,b in python you can use map function.
Suppose input is,
1
5 3
1 2 3 4 5
where 1 represent test case, 5 represent number of values and 3 represents a task value and in next line given 5 values, we can take such input using this method in PYTH 2.x Version.
testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())
You can replace 'int' in map() with another datatype needed.
add a comment |
If you need to take two integers say a,b in python you can use map function.
Suppose input is,
1
5 3
1 2 3 4 5
where 1 represent test case, 5 represent number of values and 3 represents a task value and in next line given 5 values, we can take such input using this method in PYTH 2.x Version.
testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())
You can replace 'int' in map() with another datatype needed.
If you need to take two integers say a,b in python you can use map function.
Suppose input is,
1
5 3
1 2 3 4 5
where 1 represent test case, 5 represent number of values and 3 represents a task value and in next line given 5 values, we can take such input using this method in PYTH 2.x Version.
testCases=int(raw_input())
number, taskValue = map(int, raw_input().split())
array = map(int, raw_input().split())
You can replace 'int' in map() with another datatype needed.
answered May 24 '15 at 7:42
Rishabh PrasadRishabh Prasad
1441211
1441211
add a comment |
add a comment |
All input will be through a string. It's up to you to process that string after you've received it. Unless that is, you use the eval(input()) method, but that isn't recommended for most situations anyway.
input_string = raw_input("Enter 2 numbers here: ")
a, b = split_string_into_numbers(input_string)
do_stuff(a, b)
add a comment |
All input will be through a string. It's up to you to process that string after you've received it. Unless that is, you use the eval(input()) method, but that isn't recommended for most situations anyway.
input_string = raw_input("Enter 2 numbers here: ")
a, b = split_string_into_numbers(input_string)
do_stuff(a, b)
add a comment |
All input will be through a string. It's up to you to process that string after you've received it. Unless that is, you use the eval(input()) method, but that isn't recommended for most situations anyway.
input_string = raw_input("Enter 2 numbers here: ")
a, b = split_string_into_numbers(input_string)
do_stuff(a, b)
All input will be through a string. It's up to you to process that string after you've received it. Unless that is, you use the eval(input()) method, but that isn't recommended for most situations anyway.
input_string = raw_input("Enter 2 numbers here: ")
a, b = split_string_into_numbers(input_string)
do_stuff(a, b)
answered Jun 7 '09 at 6:26
sykorasykora
60.3k95567
60.3k95567
add a comment |
add a comment |
You have to use the split()
method which splits the input into two different inputs. Whatever you pass into the split is looked for and the input is split from there. In most cases its the white space.
For example, You give the input 23 24 25
. You expect 3 different inputs like
num1 = 23
num2 = 24
num3 = 25
So in Python, You can do
num1,num2,num3 = input().split(" ")
add a comment |
You have to use the split()
method which splits the input into two different inputs. Whatever you pass into the split is looked for and the input is split from there. In most cases its the white space.
For example, You give the input 23 24 25
. You expect 3 different inputs like
num1 = 23
num2 = 24
num3 = 25
So in Python, You can do
num1,num2,num3 = input().split(" ")
add a comment |
You have to use the split()
method which splits the input into two different inputs. Whatever you pass into the split is looked for and the input is split from there. In most cases its the white space.
For example, You give the input 23 24 25
. You expect 3 different inputs like
num1 = 23
num2 = 24
num3 = 25
So in Python, You can do
num1,num2,num3 = input().split(" ")
You have to use the split()
method which splits the input into two different inputs. Whatever you pass into the split is looked for and the input is split from there. In most cases its the white space.
For example, You give the input 23 24 25
. You expect 3 different inputs like
num1 = 23
num2 = 24
num3 = 25
So in Python, You can do
num1,num2,num3 = input().split(" ")
answered Jan 24 '17 at 5:40
devDeejaydevDeejay
750816
750816
add a comment |
add a comment |
Check this handy function:
def gets(*types):
return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])
# usage:
a, b, c = gets(int, float, str)
1
Index access is not needed!return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.
– 9000
Mar 21 '17 at 20:22
add a comment |
Check this handy function:
def gets(*types):
return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])
# usage:
a, b, c = gets(int, float, str)
1
Index access is not needed!return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.
– 9000
Mar 21 '17 at 20:22
add a comment |
Check this handy function:
def gets(*types):
return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])
# usage:
a, b, c = gets(int, float, str)
Check this handy function:
def gets(*types):
return tuple([types[i](val) for i, val in enumerate(raw_input().split(' '))])
# usage:
a, b, c = gets(int, float, str)
answered Feb 16 '14 at 21:15
Mikołaj RozwadowskiMikołaj Rozwadowski
863611
863611
1
Index access is not needed!return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.
– 9000
Mar 21 '17 at 20:22
add a comment |
1
Index access is not needed!return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.
– 9000
Mar 21 '17 at 20:22
1
1
Index access is not needed!
return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.– 9000
Mar 21 '17 at 20:22
Index access is not needed!
return tuple(parser(value) for (parser, value) in zip(types, raw_input(prompt).split(' ')))
.– 9000
Mar 21 '17 at 20:22
add a comment |
The solution I found is the following:
Ask the user to enter two numbers separated by a comma or other character
value = input("Enter 2 numbers (separated by a comma): ")
Then, the string is split: n
takes the first value and m
the second one
n,m = value.split(',')
Finally, to use them as integers, it is necessary to convert them
n, m = int(n), int(m)
add a comment |
The solution I found is the following:
Ask the user to enter two numbers separated by a comma or other character
value = input("Enter 2 numbers (separated by a comma): ")
Then, the string is split: n
takes the first value and m
the second one
n,m = value.split(',')
Finally, to use them as integers, it is necessary to convert them
n, m = int(n), int(m)
add a comment |
The solution I found is the following:
Ask the user to enter two numbers separated by a comma or other character
value = input("Enter 2 numbers (separated by a comma): ")
Then, the string is split: n
takes the first value and m
the second one
n,m = value.split(',')
Finally, to use them as integers, it is necessary to convert them
n, m = int(n), int(m)
The solution I found is the following:
Ask the user to enter two numbers separated by a comma or other character
value = input("Enter 2 numbers (separated by a comma): ")
Then, the string is split: n
takes the first value and m
the second one
n,m = value.split(',')
Finally, to use them as integers, it is necessary to convert them
n, m = int(n), int(m)
answered Sep 5 '17 at 21:46
E. ZavalaE. Zavala
212
212
add a comment |
add a comment |
The easiest way that I found for myself was using split function with input
Like you have two variable a,b
a,b=input("Enter two numbers").split()
That's it.
there is one more method(explicit method)
Eg- you want to take input in three values
value=input("Enter the line")
a,b,c=value.split()
EASY..
add a comment |
The easiest way that I found for myself was using split function with input
Like you have two variable a,b
a,b=input("Enter two numbers").split()
That's it.
there is one more method(explicit method)
Eg- you want to take input in three values
value=input("Enter the line")
a,b,c=value.split()
EASY..
add a comment |
The easiest way that I found for myself was using split function with input
Like you have two variable a,b
a,b=input("Enter two numbers").split()
That's it.
there is one more method(explicit method)
Eg- you want to take input in three values
value=input("Enter the line")
a,b,c=value.split()
EASY..
The easiest way that I found for myself was using split function with input
Like you have two variable a,b
a,b=input("Enter two numbers").split()
That's it.
there is one more method(explicit method)
Eg- you want to take input in three values
value=input("Enter the line")
a,b,c=value.split()
EASY..
answered Jul 8 '17 at 10:06
user8258851user8258851
165
165
add a comment |
add a comment |
This is a sample code to take two inputs seperated by split command and delimiter as ","
>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'
Other variations of delimiters that can be used are as below :var1, var2 = input("enter two numbers:").split(',')
var1, var2 = input("enter two numbers:").split(';')
var1, var2 = input("enter two numbers:").split('/')
var1, var2 = input("enter two numbers:").split(' ')
var1, var2 = input("enter two numbers:").split('~')
add a comment |
This is a sample code to take two inputs seperated by split command and delimiter as ","
>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'
Other variations of delimiters that can be used are as below :var1, var2 = input("enter two numbers:").split(',')
var1, var2 = input("enter two numbers:").split(';')
var1, var2 = input("enter two numbers:").split('/')
var1, var2 = input("enter two numbers:").split(' ')
var1, var2 = input("enter two numbers:").split('~')
add a comment |
This is a sample code to take two inputs seperated by split command and delimiter as ","
>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'
Other variations of delimiters that can be used are as below :var1, var2 = input("enter two numbers:").split(',')
var1, var2 = input("enter two numbers:").split(';')
var1, var2 = input("enter two numbers:").split('/')
var1, var2 = input("enter two numbers:").split(' ')
var1, var2 = input("enter two numbers:").split('~')
This is a sample code to take two inputs seperated by split command and delimiter as ","
>>> var1, var2 = input("enter two numbers:").split(',')
>>>enter two numbers:2,3
>>> var1
'2'
>>> var2
'3'
Other variations of delimiters that can be used are as below :var1, var2 = input("enter two numbers:").split(',')
var1, var2 = input("enter two numbers:").split(';')
var1, var2 = input("enter two numbers:").split('/')
var1, var2 = input("enter two numbers:").split(' ')
var1, var2 = input("enter two numbers:").split('~')
answered Jul 12 '17 at 13:48
Jagannath BanerjeeJagannath Banerjee
26926
26926
add a comment |
add a comment |
For n
number of inputs declare the variable as an empty list and use the same syntax to proceed:
>>> x=input('Enter value of a and b').split(",")
Enter value of a and b
1,2,3,4
>>> x
['1', '2', '3', '4']
add a comment |
For n
number of inputs declare the variable as an empty list and use the same syntax to proceed:
>>> x=input('Enter value of a and b').split(",")
Enter value of a and b
1,2,3,4
>>> x
['1', '2', '3', '4']
add a comment |
For n
number of inputs declare the variable as an empty list and use the same syntax to proceed:
>>> x=input('Enter value of a and b').split(",")
Enter value of a and b
1,2,3,4
>>> x
['1', '2', '3', '4']
For n
number of inputs declare the variable as an empty list and use the same syntax to proceed:
>>> x=input('Enter value of a and b').split(",")
Enter value of a and b
1,2,3,4
>>> x
['1', '2', '3', '4']
edited Jun 28 '18 at 14:26
Dadep
2,33041632
2,33041632
answered Jun 28 '18 at 14:08
Premgi kumarPremgi kumar
111
111
add a comment |
add a comment |
a,b=[int(x) for x in input().split()]
print(a,b)
Output
10 8
10 8
add a comment |
a,b=[int(x) for x in input().split()]
print(a,b)
Output
10 8
10 8
add a comment |
a,b=[int(x) for x in input().split()]
print(a,b)
Output
10 8
10 8
a,b=[int(x) for x in input().split()]
print(a,b)
Output
10 8
10 8
answered Jul 7 '18 at 17:34
Siddharth jhaSiddharth jha
12
12
add a comment |
add a comment |
In Python 3, raw_input()
was renamed to input()
.
input([prompt])
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised.
So you can do this way
x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))
If you do not put two numbers using a space you would get a ValueError
exception. So that is good to go.
N.B. If you need old behavior of input()
, use eval(input())
add a comment |
In Python 3, raw_input()
was renamed to input()
.
input([prompt])
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised.
So you can do this way
x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))
If you do not put two numbers using a space you would get a ValueError
exception. So that is good to go.
N.B. If you need old behavior of input()
, use eval(input())
add a comment |
In Python 3, raw_input()
was renamed to input()
.
input([prompt])
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised.
So you can do this way
x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))
If you do not put two numbers using a space you would get a ValueError
exception. So that is good to go.
N.B. If you need old behavior of input()
, use eval(input())
In Python 3, raw_input()
was renamed to input()
.
input([prompt])
If the prompt argument is present, it is written to standard output
without a trailing newline. The function then reads a line from input,
converts it to a string (stripping a trailing newline), and returns
that. When EOF is read, EOFError is raised.
So you can do this way
x, y = input('Enter two numbers separating by a space: ').split();
print(int(x) + int(y))
If you do not put two numbers using a space you would get a ValueError
exception. So that is good to go.
N.B. If you need old behavior of input()
, use eval(input())
answered Jul 22 '18 at 7:18
unclexounclexo
1,295511
1,295511
add a comment |
add a comment |
I tried this in Python 3 , seems to work fine .
a, b = map(int,input().split())
print(a)
print(b)
Input : 3 44
Output :
3
44
add a comment |
I tried this in Python 3 , seems to work fine .
a, b = map(int,input().split())
print(a)
print(b)
Input : 3 44
Output :
3
44
add a comment |
I tried this in Python 3 , seems to work fine .
a, b = map(int,input().split())
print(a)
print(b)
Input : 3 44
Output :
3
44
I tried this in Python 3 , seems to work fine .
a, b = map(int,input().split())
print(a)
print(b)
Input : 3 44
Output :
3
44
edited Oct 26 '18 at 18:21
Madhur Bhaiya
19.6k62236
19.6k62236
answered Oct 26 '18 at 18:03
user10564553user10564553
1
1
add a comment |
add a comment |
Two inputs separated by space:
x,y=input().split()
add a comment |
Two inputs separated by space:
x,y=input().split()
add a comment |
Two inputs separated by space:
x,y=input().split()
Two inputs separated by space:
x,y=input().split()
answered Nov 16 '18 at 18:02
ZeinabZeinab
7915
7915
add a comment |
add a comment |
This solution is being used for converting multiple string like ("22 44 112 2 34") to an integer list and you can access to the values in a list.
n = input("") # Like : "22 343 455 54445 22332"
if n[:-1] != " ":
n += " "
char = ""
list =
for i in n :
if i != " ":
char += i
elif i == " ":
list.append(int(char))
char = ""
print(list) # Be happy :))))
add a comment |
This solution is being used for converting multiple string like ("22 44 112 2 34") to an integer list and you can access to the values in a list.
n = input("") # Like : "22 343 455 54445 22332"
if n[:-1] != " ":
n += " "
char = ""
list =
for i in n :
if i != " ":
char += i
elif i == " ":
list.append(int(char))
char = ""
print(list) # Be happy :))))
add a comment |
This solution is being used for converting multiple string like ("22 44 112 2 34") to an integer list and you can access to the values in a list.
n = input("") # Like : "22 343 455 54445 22332"
if n[:-1] != " ":
n += " "
char = ""
list =
for i in n :
if i != " ":
char += i
elif i == " ":
list.append(int(char))
char = ""
print(list) # Be happy :))))
This solution is being used for converting multiple string like ("22 44 112 2 34") to an integer list and you can access to the values in a list.
n = input("") # Like : "22 343 455 54445 22332"
if n[:-1] != " ":
n += " "
char = ""
list =
for i in n :
if i != " ":
char += i
elif i == " ":
list.append(int(char))
char = ""
print(list) # Be happy :))))
answered Nov 24 '18 at 21:33
Ali MontajebiAli Montajebi
1
1
add a comment |
add a comment |
You can do this way
a, b = map(int, input().split())
OR
a, b = input().split()
print(int(a))
OR
MyList = list(map(int, input().split()))
a = MyList[0]
b = MyList[1]
add a comment |
You can do this way
a, b = map(int, input().split())
OR
a, b = input().split()
print(int(a))
OR
MyList = list(map(int, input().split()))
a = MyList[0]
b = MyList[1]
add a comment |
You can do this way
a, b = map(int, input().split())
OR
a, b = input().split()
print(int(a))
OR
MyList = list(map(int, input().split()))
a = MyList[0]
b = MyList[1]
You can do this way
a, b = map(int, input().split())
OR
a, b = input().split()
print(int(a))
OR
MyList = list(map(int, input().split()))
a = MyList[0]
b = MyList[1]
answered Dec 20 '18 at 0:01
vishwarajvishwaraj
33924
33924
add a comment |
add a comment |
if we want to two inputs in a single line so, the code is are as follows enter code here
x,y=input().split(" ")
print(x,y)
after giving value as input we have to give spaces between them because of split(" ") function or method.
but these values are of string type if to perform some arithmetic operations we have to convert the type of x,y are as follows
int(x),int(y)
we can do this also with the help of list in python.enter code here
list1=list(map(int,input().split()))
print(list1)
this list gives the elements in int type
New contributor
add a comment |
if we want to two inputs in a single line so, the code is are as follows enter code here
x,y=input().split(" ")
print(x,y)
after giving value as input we have to give spaces between them because of split(" ") function or method.
but these values are of string type if to perform some arithmetic operations we have to convert the type of x,y are as follows
int(x),int(y)
we can do this also with the help of list in python.enter code here
list1=list(map(int,input().split()))
print(list1)
this list gives the elements in int type
New contributor
add a comment |
if we want to two inputs in a single line so, the code is are as follows enter code here
x,y=input().split(" ")
print(x,y)
after giving value as input we have to give spaces between them because of split(" ") function or method.
but these values are of string type if to perform some arithmetic operations we have to convert the type of x,y are as follows
int(x),int(y)
we can do this also with the help of list in python.enter code here
list1=list(map(int,input().split()))
print(list1)
this list gives the elements in int type
New contributor
if we want to two inputs in a single line so, the code is are as follows enter code here
x,y=input().split(" ")
print(x,y)
after giving value as input we have to give spaces between them because of split(" ") function or method.
but these values are of string type if to perform some arithmetic operations we have to convert the type of x,y are as follows
int(x),int(y)
we can do this also with the help of list in python.enter code here
list1=list(map(int,input().split()))
print(list1)
this list gives the elements in int type
New contributor
edited Feb 17 at 13:16
New contributor
answered Feb 17 at 12:39
JATIN BEHUNEJATIN BEHUNE
12
12
New contributor
New contributor
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f961263%2ftwo-values-from-one-input-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Very similar recent question: stackoverflow.com/questions/959412/parsing-numbers-in-python
– Nicolas Dumazet
Jun 7 '09 at 5:51