Lecture

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.

Using the input() Function
# 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 from input() is returned as a string, you may need to convert it 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 to an integer or float.

In Python, use int() to convert a string to an integer, and float() to convert it to a floating-point number.

The example below shows how to convert user input to an integer using the int() function.

Data Type Conversion of Input
age_input = input("Please enter your age: ") age = int(age_input) # Convert string to integer using int() print(f"You are {age} years old.")
Quiz
0 / 1

What is the return type of the input() function always?

Integer

Float

String

Boolean

Lecture

AI Tutor

Design

Upload

Notes

Favorites

Help

Code Editor

Run
Generate

Execution Result