I'm working on a code right now that a part of it requires to ask the user for 3 different numbers in one line [ could be any number of digits in each number]. Say I ask the user for the input and he enters : "31 722 9191". A space is required between the numbers. How would you go about separating these numbers and assigning a variable to each one of them. So for example 31 would be "A", 722 would be "B" and so on... What I've got so far:
user_input = input[" Please enter the numbers: "]
Thanks !
asked Sep 15, 2013 at 2:18
Use a combination of split and sequence unpacking.
user_input = user_input[" Please enter the numbers: "]
a, b, c = user_input.split[]
split
will take your string of numbers, say "x y z", and turn it into a list of elements in the string where the elements are all the words in the
string that are separated by spaces. Thus split
will yield the string ['x', 'y', 'z'] for input 'x y z'.
Since a list is a form of sequence, its elements can be "unpacked" and assigned to a list of variables of your choosing.
answered Sep 15, 2013 at 2:19
ShashankShashank
13.4k5 gold badges35 silver badges61 bronze badges
2
x = [input["Enter 3 user inputs: "].split[]]
a = int[x[0]]
b = int[x[1]]
c = int[x[2]]
print[f"A: {a}, B: {b}, C: {c}"]
answered Mar 24, 2019 at 11:58
0
You can also do like this
a, b, c = [int[x] for x in input["Please enter the numbers: "].split[]]
answered May 14, 2021 at 11:45
Nithin RNithin R
6016 silver badges6 bronze badges
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
The developer often wants a user to enter multiple values or inputs in one line. In C++/C user can take multiple inputs in one line using scanf but in Python user can take multiple values or inputs in one line by two methods.
- Using split[] method
- Using List comprehension
Using split[] method :
This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a
separator. Generally, users use a split[] method to split a Python string but one can use it in taking multiple inputs.
Syntax :
input[].split[separator, maxsplit]
Example :
Python3
x, y
=
input
[
"Enter two values: "
].split[]
print
[
"Number of boys: "
, x]
print
[
"Number of girls: "
, y]
print
[]
x, y, z
=
input
[
"Enter three values: "
].split[]
print
[
"Total number of students: "
, x]
print
[
"Number of boys is : "
, y]
print
[
"Number of girls is : "
, z]
print
[]
a, b
=
input
[
"Enter two values: "
].split[]
print
[
"First number is {} and second number is {}"
.
format
[a, b]]
print
[]
x
=
list
[
map
[
int
,
input
[
"Enter multiple values: "
].split[]]]
print
[
"List of students: "
, x]
Output:
Using List comprehension :
List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user.
Example:
Python3
x, y
=
[
int
[x]
for
x
in
input
[
"Enter two values: "
].split[]]
print
[
"First Number is: "
, x]
print
[
"Second Number is: "
, y]
print
[]
x, y, z
=
[
int
[x]
for
x
in
input
[
"Enter three values: "
].split[]]
print
[
"First Number is: "
, x]
print
[
"Second Number is: "
, y]
print
[
"Third Number is: "
, z]
print
[]
x, y
=
[
int
[x]
for
x
in
input
[
"Enter two values: "
].split[]]
print
[
"First number is {} and second number is {}"
.
format
[x, y]]
print
[]
x
=
[
int
[x]
for
x
in
input
[
"Enter multiple values: "
].split[]]
print
[
"Number of list is: "
, x]
Output :
Note: The above examples take input separated by spaces. In case we wish to take input separated by comma [, ], we can use the following:
Python3
x
=
[
int
[x]
for
x
in
input
[
"Enter multiple value: "
].split[
","
]]
print
[
"Number of list is: "
, x]
Please see //ide.geeksforgeeks.org/BHf0Cxr4mx for a sample run.