How to take a line of integers as input in python

In Python 2, you could write:

numbers = map[int, raw_input[].split[]]

This reads a line, splits it at white spaces, and applies int[] to every element of the result.

If you were using Python 3, the equivalent expression would be:

numbers = list[map[int, input[].split[]]]

or

numbers = [int[n] for n in input[].split[]]

You can use a list comprehension to take n inputs in one line in Python. The input string is split into n parts, then the list comp creates a new list by applying int[] to each of them.

Simple example code

n = 2  # how many numbers to accept
numbers = [int[num] for num in input[].split[" ", n-1]]

print[numbers]

Output:

The following snippet will map the single line input separated by white space into a list of integers

lst = list[map[int, input[].split[]]]

print[lst]

Output:

1 2 3
[1, 2, 3]

How to take multiple inputs of different data types in one line in Python?

Answer: Example take 2 input values.

x, y = input["Enter a two value: "].split[]

print[x, y]

Output:

Enter a two value: 1 X
1 X

OR

score, name = int[input['Enter Score: ']], input['Enter name:']

print[score]
print[name]

Do comment if you have any doubts and suggestions on this Python input topic.

Note: IDE: PyCharm 2021.3.3 [Community Edition]

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Use input[], map[] and split[] function to take space-separated integer input in Python 3. You have to use list[] to convert the map to a list.

list[map[int,input[].split[]]] 

Where:

  • input[] accepts a string from STDIN.
  • split[] splits the string about whitespace character and returns a list of strings.
  • map[] passes each element of the 2nd argument to the first argument and returns a map object

Simple example code stage user multiple integers input, each separated space.

print["Enter the numbers: "]

inp = list[map[int, input[].split[]]]

print[inp]

Output:

Do comment if you have any doubts and suggestions on this Python input program.

Note: IDE: PyCharm 2021.3.3 [Community Edition]

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Integer Input From Command Line In Python 2

Python raw_input[] allows taking input from command line, but by default all the inputs are treated as strings.

In [1]:

userinput = raw_input["Enter Integer Number!\n"]
print["You entered %d"%userinput]

---------------------------------------------------------------------------
TypeError                                 Traceback [most recent call last]
 in []
      1 userinput = raw_input["Enter Integer Number!\n"]
----> 2 print["You entered %d"%userinput]

TypeError: %d format: a number is required, not str

The reason we got above error is "userinput" variable contains a string but not a number.

To fix this, we will have to convert the input to integer before assigning to a variable.

In [2]:

userinput = int[raw_input["Enter Integer Number!\n"]]
print["You entered %d"%userinput]

Enter Integer Number!
5
You entered 5

Float Input From Command Line In Python 2

Similarly we can tweak our previous code to take a Floating point number as input.

In [3]:

userinput = float[raw_input["Enter Floating Point Number!\n"]]
print["You entered %f"%userinput]

Enter Floating Point Number!
5.6
You entered 5.600000

Integer Input From Command Line In Python 3

Similarly we can use above code snippets in Python 3 by replacing the Python input function raw_input[] with input[].

In [4]:

userinput = int[input["Enter Integer Number!\n"]]
print["You entered %d"%userinput]

Enter Integer Number!
5
You entered 5

Float Input From Command Line In Python 3

In [5]:

userinput = float[raw_input["Enter Floating Point Number!\n"]]
print["You entered %f"%userinput]

Enter Floating Point Number!
5.6
You entered 5.600000

Related Notebooks

How do you input an integer in one line in Python?

One solution is to use raw_input[] two times. Note that we don't have to explicitly specify split[' '] because split[] uses any whitespace characters as a delimiter as default.

How do you take just the input of an integer?

Shortest. We use here standard shorthand for "OR" operator e.g 9 | 2 = 11 in binary: 0b1001 | 0b1010 = 0b1011 . This operator first cast numbers to integers in implicit way and then do OR. But because OR with zero don't change anything so number is cast to integer.

How do you take an input from a single line in Python?

To take list input in Python in a single line use input[] function and split[] function. Where input[] function accepts a string, integer, and character input from a user and split[] function to split an input string by space.

How do you take two integer inputs in the same line in Python?

a] split [] split[ ] function helps us get multiple inputs from the user and assign them to the respective variables in one line. This function is generally used to separate a given string into several substrings.

Chủ Đề