Python 3 uses in-built input() function to accept input from keyword. By default, it takes the input as string.
Let’s try to take a word as our input :
s = input('Enter the string :') print(s)
Enter the string : Code
Once this value gets stored in the variable ‘s’, it can be converted to other datatypes like int, float etc.
s = input('Enter the number :') n = int(s) #string to int conversion print(s)
Enter the number : 654
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.
Enter the number : 45
Similarly we use float(input()) to take floating value as an input.
s = float(input('Enter the floating number :')) print(s)
Enter the floating number : 45.65
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)
Taking a list of ‘n’ integers as input :
Printing a formatted output :
1. Let us assume that we have to print values of two variables – a=10 b=20 as 10—-20, We can do this by : print(a,b,sep=’—‘)
2. The output displayed by the print() method can be formatted as you like. ‘%’ operator is used for this purpose. It joins a string with a variable or value. Example :
print(“string” % (variable-list))