In Python, you can ask the user to type something while the program is running using the built-in input() function…
User input is essential for making your programs interactive—for example, forms, quizzes, calculators, and games.
input() returns a string – even if the user types numbers.int() or float() to convert the string to numbers.input("...") explains what the user should type.input() with split() to read more than one value at a time.+ with strings or f-strings like f"Hello {name}".Basic syntax of the input() function:
input() Syntax
# store what the user types in a variable
variable_name = input("Prompt message shown to the user: ")
To use a value as an integer or float, wrap input() with a type conversion function:
# read age as an integer
age = int(input("Enter your age: "))
# read height as a float
height = float(input("Enter your height in meters: "))
Ask the user for their name and greet them:
# ask for the user's name
name = input("Enter your name: ")
# greet the user
print("Hello, " + name)
Read an age as a number so you can perform numeric operations:
# read the age as text
age = input("Enter your age: ")
age = int(age) # convert string to integer
# print the result
print(f"You are {age} years old.")
Read two numbers separated by a space and print their sum:
# read two numbers at once
a, b = input("Enter two numbers separated by space: ").split()
# convert them to integers
a = int(a)
b = int(b)
# print their sum
print("Sum =", a + b)
Read a decimal value such as height and format the output:
# input float value
height = float(input("Enter your height in meters: "))
# show with 2 decimals
print(f"Your height is {height:.2f} meters")
Enter your name:AlexHello, Alex18input() returns "18"int("18") → 18You are 18 years old.5 7121.7351.7351.73split() for multiple inputs.