How to Take Input from User in Python?

There are many ways to take input in Python. Python has the in-built input() method to receive input data from the users in string format. I will share some useful tips in this post about how to get multiple inputs from users in various formats such as arrays, lists, strings, etc.

How to Take Input in Python?

We can call the input() method to get the input data from the users. The program will execute only after the user enters valid data or presses the enter key.

The data type of the value entered by the user is always a string.

Example

age = input('What is your age? ')
print(f'Your age is {age}')

Output

What is your age? 32
Your age is 32

How to Take a String Input in Python?

By default, any value entered by the user in the terminal is converted into the string format. So, you don’t need to do any extra work to get the string input.

Example

age = input('What is your age? ')
print(f'Your age is {age}')
print(type(age))

Output

What is your age? 28
Your age is 28
<class 'str'>

from the above output, you would have already understood that the data type of the user entered data is string even though the user entered a number.

How to Take Multiple Input in Python?

If you want limited information from the user, you can create a variable for each data, get the input from the user and store it.

Example 1

name = input('Enter your name: ')
gender = input('Your gender: ')
print(f'Your name is {name}')
print(f'Your gender is {gender}')

Output

Enter your name: Jane Doe
Your gender: Female
Your name is Jane Doe
Your gender is Female

If you would like to receive the data from the user dynamically, you can use the string split() method to get multiple inputs and store them in different variables. Here is the working example.

Example 2

age, city = input('Enter your age & city separated by a space: ').split()
print(f'Your age is {age}')
print(f'Your city is {city}')

Output

Enter your age & city separated by a space: 30 Ohio
Your age is 30
Your city is Ohio

Example 3

In this example, we ask users to enter the details separated by a comma and we split them to retrieve the right values from the input string.

name, gender = input('Enter your name & gender separated by a comma: ').split(',')
print(f'Your name is {name}')
print(f'Your gender is {gender}')

Output

Enter your name & gender separated by a comma: John Doe,Male
Your name is John Doe
Your gender is Male

How to Take Array Input in Python?

Python has the built-in data type list to store collections of data. It works in a similar way to Array in JavaScript. In order to get the array as an input from the user in Python, we need to convert the user-entered data into the list.

Example

languages = input('What languages do you speak? ')
languages_list = languages.split(',') # you should prompt user to enter values separated by a comma
print(languages_list)
print(type(languages_list))

Output

We converted the user-entered values into a list(array) by using the string split() method.

What languages do you speak? English,Polish,German,French
['English', 'Polish', 'German', 'French']
<class 'list'>

How to Take Integer Input in Python?

We can’t take integer inputs from users directly. So, we need to get the data first and convert it into a valid integer later.

Example

age = input('Enter your age: ')
age_int = int(age) # convert into an integer
print(age) # it's in string format
print(type(age))
print(age_int) # now it's an integer
print(type(age_int))

Output

Enter your age: 28
28
<class 'str'>
28
<class 'int'>

How to Take List as Input in Python?

Method 1: Using split Method

We should ask the user to enter the data in the Python list format to automatically convert it into a list. The comma is most widely used to separate the data values and combine them into a list later.

Example

languages = input('What languages do you speak?(enter the values separated by a comma): ')
languages_list = languages.split(',')
print(languages_list)
print(type(languages_list))

Output

What languages do you speak?(enter the values separated by a comma): English,French,Italian,French
['English', 'French', 'Italian', 'French']
<class 'list'>

Method 2: Using for Loop

In this method, we will ask the user to enter a value and append it to an existing list. It’s very simple method.

Example

total = 0
list_of_numbers = []
n = input('How many numbers do you want to add? ')
n = int(n)  # converted as an integer
for i in range(0, n):
    num = int(input('Enter number to add: '))  # converted as an integer
    list_of_numbers.append(num)  # adding the number to list
    total += num  # adding number for calculating total
print('list of numbers = ', list_of_numbers)
print('total = ', total)

Output

How many numbers do you want to add? 4
Enter number to add: 1
Enter number to add: 2
Enter number to add: 3
Enter number to add: 4
list of numbers =  [1, 2, 3, 4]
total =  10