Goal
You’ll be able to get user input, display output, and convert between types correctly — and understand why a specific, extremely common beginner error happens with input().
Learn
print() displays output to the console:
print("Hello, world!")
print("Age:", 25) input() pauses the program and waits for the user to type something, returning what they typed:
name = input("What's your name? ")
print(f"Hello, {name}!") Here’s the genuinely important detail: input() always returns a string, no matter what the user actually types — even if they type numbers. This means:
age = input("How old are you? ")
next_year = age + 1 # This will crash! Even if the user types “25”, age holds the string "25", not the integer 25 — attempting "25" + 1 raises a TypeError, since you can’t add a string and an integer directly.
The fix is explicit type conversion, using built-in functions:
age = int(input("How old are you? ")) # converts to int immediately
next_year = age + 1 # works correctly now int(), float(), and str() convert values between types explicitly. This single detail — that input() always returns a string — is one of the most common sources of beginner errors in early Python programs, precisely because the mistake is invisible until you actually try to do math with the result.
Decision Task
A beginner writes quantity = input("How many items? ") then later tries total = quantity * price where price is a float, and gets unexpected behavior (not even a crash, but a strange repeated result). Before reading on: what’s actually happening, given that Python allows multiplying a string by an integer for a different purpose (like “ab” * 3 giving “ababab”)?
Show Answer
If price happens to be a whole-number-like float such as 3.0, Python would actually raise a TypeError trying to multiply a string by a float directly (string repetition only works with integers, not floats) — but if quantity were being multiplied by an actual int elsewhere, Python’s string repetition behavior ("3" * 4 giving "3333") could silently produce a nonsensical repeated string instead of a real calculation, since quantity is still a string, not a number, exactly because input() never auto-converts types.
Common Mistake
Forgetting that input() always returns a string, and attempting arithmetic directly on the result without converting it first. This is genuinely one of the most common early Python bugs, precisely because the code often looks completely correct at a glance — the error only appears once the program actually runs and hits real arithmetic on what turns out to be text, not a number.
Practice Questions
1. Write code that asks the user for their age using input(), correctly converted to an integer immediately.
Show Answer
age = int(input("What is your age? "))
2. What type does input() always return, regardless of what the user types?
Show Answer
str (string) — always, even if the user types only digits.
3. Why does age = input(“Age: “) followed by age + 1 raise a TypeError?
Show Answer
Because age holds a string (input() always returns str), and you can’t directly add a string and an integer together with the + operator without explicit conversion first.
4. What three built-in functions convert between types explicitly, covered in this lesson?
Show Answer
int(), float(), and str().
5. True or False: input() automatically detects if the user typed a number and converts it to int or float accordingly.
Show Answer
False — input() always returns a plain string with zero automatic type detection; any conversion must be done explicitly by the programmer.
Try It Yourself
Without looking back, write a short program that asks the user for two numbers using input(), converts both to floats, and prints their sum using an f-string.
Show Answer
num1 = float(input("Enter first number: "))\nnum2 = float(input("Enter second number: "))\nprint(f"Sum: {num1 + num2}") — both inputs must be explicitly converted to float before adding, since input() always returns strings.
Quick Check
1. What does print() do?
Show Answer
Displays output to the console.
2. What type does input() always return?
Show Answer
str (string), regardless of what the user actually types.
3. What function converts a string to an integer?
Show Answer
int()
4. Why is forgetting to convert input() a common source of bugs?
Show Answer
Because the code often looks correct until it actually runs and hits real arithmetic on what turns out to be a string, not a number, causing a TypeError or unexpected behavior.
5. What function converts a value to a string explicitly?
Show Answer
str()