Using the input
Function to Receive User Input
The input()
function is used to receive input from the user, and it always returns the input data as a string
.
# Receive input from the user user_input = input("Please enter your name: ") # Output the entered data print(f"Welcome, {user_input}!")
Converting the Data Type of Input
Since the data obtained from input()
is returned as a string, you may need to convert the input to another data type for arithmetic operations or logical comparisons.
For instance, if you want to receive a number from the user for mathematical operations, the input needs to be converted into an integer
or float
.
In Python, you can convert a given value to an integer using the int()
function and to a float using the float()
function.
The following example demonstrates using the int()
function to convert the age entered by the user into an integer.
age_input = input("Please enter your age: ") age = int(age_input) # Convert string to integer using int() print(f"You are {age} years old.")
What is the return type of the input() function always?
Integer
Float
String
Boolean
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result