Python 3 uses in-built input() function to accept input from keyword. By default, it takes the input as a string.
Let’s try to take a word as our input :
s = input('Enter the string :')
print('You have entered : ',s)
O/P :
Enter the string : CODEOFGEEKS
You have entered : CODEOFGEEKS
Once this value gets stored in the variable ‘s’, it can be converted to other datatypes like int, float etc.
s = input('Enter the string :')
print('You have entered : ', s, type(s))
# after typecasting -> changing str to int
n = int(s)
print('Typecasted Result : ',n, type(n))
O/P :
Enter the string :100
You have entered : 100
Typecasted Result : 100
Also, we can use this int() function before input() to accept integer value.
s = int(input('Enter the number :'))
print(type(s)) # checking value type stored by s.
Similarly we use float(input()) to take floating value as an input.
s = float(input('Enter the floating number :'))
print(s)
Taking inputs :
s = input() // taking string as input
n = int(input()) // taking int as input
b = bool(input()) // taking boolean value as input
l = list(input().split(‘,’)) // taking list as a input where elements are seperated by comma
s = tuple(input().split(‘,’)) // taking tuple as a input where elements are seperated by comma
Taking Multiple input in one line :
a,b = input().split(separator, maxsplit)
a, b = input('Enter the values for a, b : ').split(',')
print('Values entered : ',a, b)
O/P :
Enter the values for a, b : 4,8
Values entered : 4 8
Taking a list of ‘n’ integers as input :
inp = list(map(int, input('Enter multiple inputs : ').strip().split()))[:5]
print('Values entered : ', inp)
O/P :
Enter multiple inputs : 4 4 6 2 6 9
Values entered : [4, 4, 6, 2, 6]
That’s all, see you in the next lecture.