Python read input from the user
Sometimes you may need to take input from the user, the easiest way to take input from the user by console, In this tutorial section, you will learn about taking input from the user by console.
Objectives
-
Python get user input from console
-
Python gets int, float, str user input from console
Python get user input from console
# Get input from console
print("Please enter something", end=" : ")
entered_input = input()
# output
print("Get from user", end=" >> ")
print(entered_input)
Output
Please enter something : Bismillah
Get from user >> Bismillah
Process finished with exit code 0
input() : Take input from user using console
Python get user input from console
# Get input from console
integer_input = int(input("Enter Integer : "))
float_input = float(input("Enter Float : "))
string_input = str(input("Enter String : "))
print("\n-------- Output -------- \n")
# output
print("Integer value : " + str(integer_input))
print("Float value : " + str(float_input))
print("String value : " + string_input)
Output
Enter Integer : 10
Enter Float : 20
Enter String : HMTMCSE
-------- Output --------
Integer value : 10
Float value : 20.0
String value : HMTMCSE
Process finished with exit code 0
-
int(input("Enter Integer : ")) : Convert/Cast raw input to integer
-
float(input("Enter Float : ")) : Convert/Cast raw input to float
-
str(input("Enter String : ")) : Convert/Cast raw input to string