Photo by Fabian Grohs on Unsplash

Inputting with Python input statement for Beginners

Kishor Keshav
3 min readJun 12, 2020

--

Many beginners in Python spend a lot of time for knowing how to input multiple data items belonging to different data types in Python like strings, integers, and floats, and data structures like lists, dictionaries, and tuples. Beginners always struggle when they need to input multiple values from a single line of input. This article will be helpful in saving time for searching the ways to do this smartly. Note that there are other ways to do it, but I have listed the simpler and cleaner ways here. The sample code given here is self explanatory with leading comments.

The map function used here has a syntax as map(func, iterable). An iterable is any Python object like list, tuple, etc. that is capable of returning its members one at a time; and func is a function which is applied to each element of the given iterable. The split() function separates input line entered by the user into a list of different tokens separated by the given separator. In case of no separator, it takes any white space(spaces, tab, etc) as a separator by default. In the first example below, split() function returns a list of strings(str)after breaking the given input line by the default separator like a tab or a space, and by applying str() on each token in the input line.

In [2]:

#inputting strings separated by white spacesfname,mname,lname=map(str,input("Enter your full name:").split())Enter your full name:Abhay M Divan

In [3]:

print(fname,' ',mname,' ',lname)Abhay   M   Divan

In [4]:

#inputting integer values separated by white spaces on new linea,b=map(int,input("Enter two numbers:").split())Enter two numbers:24 -23

In [5]:

print(a,b)24 -23

In [6]:

#inputting float values separated by commas on new linex,y,z=map(float,input("Enter three numbers separated by comma:").split(","))Enter three numbers separated by comma:323,-343.9,0

In [7]:

print(x,y,z)323.0 -343.9 0.0

In [8]:

#inputting integers as a list separated by white spaces and terminated by enter keyprint("Enter numbers separated by spaces:")lstnum = list(map(int,input().split()))Enter numbers separated by spaces:
495 -99 0 0 999

In [9]:

print(lstnum)[495, -99, 0, 0, 999]

In [10]:

#other way to do the same tasklstnum1 = [int(x) for x in input().split()]496 -99 0 0 999

In [11]:

print(lstnum1)[496, -99, 0, 0, 999]

In [12]:

#inputting strings as a list separated by comma and terminated by enter keyprint("Enter strings separated by commas:")lststr = list(map(str,input().split(",")))Enter strings separated by commas:
Nagpur, Mumbai,Pune

In [13]:

print(lststr)['Nagpur', ' Mumbai', 'Pune']

In [14]:

#other way to do the samelststr1 = [str(x) for x in input().split(",")]Nagpur,Mumbai,Pune

In [15]:

print(lststr1)['Nagpur', 'Mumbai', 'Pune']

In [16]:

#inputting dictionary as a key-value pairFruitPrices = dict()user_input = input("Enter Fruit and Price per kg separated by commas: ")key, value = user_input.split(",")FruitPrices[key] = int(value)Enter Fruit and Price per kg separated by commas: Apple,150

In [17]:

print(FruitPrices){'Apple': 150}

In [18]:

#inputting multiple dictionary items#Suppose you want a dictionary of letters and their values#take care to enter same number of items for keys and valuesprint("Enter letters separated by commas:")letters = [str(x) for x in input().split(",")]print("Enter numbers separated by commas:")numbers = [int(x) for x in input().split(",")]d = {letters[i]: numbers[i] for i in range(len(numbers))}Enter letters separated by commas:
a,b,c,d,e
Enter numbers separated by commas:
10,20,30,40,50

In [19]:

print(d){'a': 10, 'b': 20, 'c': 30, 'd': 40, 'e': 50}

In [20]:

#inputting multiple values in a tuple from a single line terminated by enter keyt=tuple(map(int,input().split(',')))13949,393909,30002,29290239

In [21]:

print(t)(13949, 393909, 30002, 29290239)

Hope this quick read is useful to you as a beginner. Have a fun coding with Python.

--

--