
Python
The input() function lets you ask the user to type information into your program, making it interactive. Everything input() gets from the user is a string, so you often need to convert it to the right type (number, boolean, etc.) before using it. Understanding how to get and process user input is essential for building real programs that interact with people.
The input() function pauses your program and waits for the user to type something and press Enter. It returns whatever the user typed as a string. You typically store this string in a variable for later use, and convert it to the appropriate type if needed (like int for numbers).
# Ask user for their name
name = input("What's your name? ")
print(f"Hello, {name}!")
# Ask for age (gets string, might need conversion)
age_str = input("How old are you? ")
print(f"You are {age_str} years old")
# input() always returns a string
age = input("Enter your age: ")
print(f"Type: {type(age)}") # <class 'str'>, not int!Since input() always returns a string, you need to convert it if you want to do calculations or comparisons with numbers. Use int() to convert to integer or float() to convert to decimal. Always be prepared for the possibility that the user might type something that can't be converted.
# Get a number from user and do math
age_str = input("Enter your age: ")
age = int(age_str) # Convert string to integer
# Now we can do calculations
birth_year = 2024 - age
print(f"You were born around {birth_year}")
# Get a price and calculate total
price_str = input("Enter price: $")
price = float(price_str) # Convert to float
quantity = 3
total = price * quantity
print(f"Total: ${total:.2f}")You can combine input() with variables and string formatting to create engaging programs. Ask multiple questions, process the answers, and display results in a clear format. This is how most real programs gather information from users.
# Personal information program
print("=== Personal Information ===")
name = input("Enter your name: ")
age = int(input("Enter your age: "))
city = input("Enter your city: ")
# Display organized profile
profile = f"""
Name: {name}
Age: {age} years old
City: {city}
Born around: {2024 - age}
"""
print(profile)print("=== Simple Calculator ===")
# Get two numbers from user
num1_str = input("Enter first number: ")
num2_str = input("Enter second number: ")
# Convert to float
num1 = float(num1_str)
num2 = float(num2_str)
# Perform calculations
sum_result = num1 + num2
product = num1 * num2
average = (num1 + num2) / 2
# Display results
print(f"Sum: {sum_result}")
print(f"Product: {product}")
print(f"Average: {average}")print("=== Shopping Cart ===")
# Get items and prices
item = input("What item are you buying? ")
price_str = input(f"Price of {item}: $")
quantity_str = input("Quantity: ")
# Convert to numbers
price = float(price_str)
quantity = int(quantity_str)
# Calculate total
subtotal = price * quantity
tax = subtotal * 0.08
total = subtotal + tax
# Display receipt
print(f"\nItem: {item}")
print(f"Price: ${price:.2f}")
print(f"Quantity: {quantity}")
print(f"Subtotal: ${subtotal:,.2f}")
print(f"Tax: ${tax:,.2f}")
print(f"Total: ${total:,.2f}")print("=== Create Your Bio ===\n")
# Gather information
first_name = input("First name: ")
last_name = input("Last name: ")
birth_year_str = input("Birth year: ")
city = input("City: ")
hobby = input("Favorite hobby: ")
# Convert age
birth_year = int(birth_year_str)
age = 2024 - birth_year
# Create bio
bio = f"""
{'='*40}
BIO
{'='*40}
Name: {first_name} {last_name}
Age: {age}
City: {city}
Hobby: {hobby}
{'='*40}
"""
print(bio)While advanced input validation requires error handling (which we'll learn later), you can do simple checks now. For example, you can verify that input is not empty, or warn users if they enter something unexpected.
# Simple check for empty input
name = input("Enter your name: ")
if name == "":
print("Name cannot be empty!")
else:
print(f"Hello, {name}!")
# Check if input is reasonable
age_str = input("Enter your age: ")
age = int(age_str)
if age < 0 or age > 150:
print("That age seems unusual!")
else:
print(f"You are {age} years old")| Concept | Remember |
|---|---|
| input() | Gets text from user |
| Always String | input() returns string, convert if needed |
| int() | Convert string to integer |
| float() | Convert string to decimal |
| Prompt | Put helpful text in input() |
| Variables | Store input in variables |
| Processing | Convert and use input appropriately |
You've learned all the basics! Now let's put it all together with best practices for writing clean, professional code!
Ready to practice? Try challenges or take the quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward