User Input
User Input
Many programs become useful when they interact with the user. Python allows you to collect information from the keyboard using the input() function.
What Is User Input?
The input() function pauses the program and waits for the user to type something.
name = input("Enter your name: ")
print(name)
How input() Works
The text inside input() is called a prompt. It tells the user what to enter.
city = input("Enter your city: ")
print("You live in", city)
Input Is Stored as Text
By default, everything entered with input() is stored as a string.
age = input("Enter your age: ")
print(type(age))
Output:
<class 'str'>
Converting Input
Use int() or float() when working with numbers.
age = int(input("Enter your age: "))
print(age + 1)
price = float(input("Enter price: "))
print(price * 2)
Multiple Inputs
You can collect several values in one program.
name = input("Enter name: ")
age = int(input("Enter age: "))
country = input("Enter country: ")
print(name)
print(age)
print(country)
Code Along
name = input("What is your name? ")
favorite_food = input("What is your favorite food? ")
print("Hello", name)
print("You like", favorite_food)
Mini Challenge
Create a program that asks the user for:
- Name
- Age
- Favorite color
Then print all answers nicely.
Real World Use Case
Login forms, registration pages, surveys, calculators, and search tools all rely on user input.
Quiz
- What function is used to collect input?
- What type does
input()return by default? - Why do we use
int()with age? - What is a prompt?
Assignment
Build a simple profile app that asks for a user’s name, age, country, and hobby, then displays the information.
Summary
You learned how to collect user input, use prompts, convert input into numbers, and build interactive Python programs.