Python input() and Type Conversion: int(), float(), str()
Python 3.14 Β·
β verified by execution on 2026-07-19
So far your programs have only talked (with print). Now theyβll listen. The input()
function pauses your program, waits for the user to type a line and press Enter, and gives
you back what they typed:
name = input("What is your name? ")print(f"Hello, {name}!")
Run that in real Python and it asks for your name, then greets you. (Youβll do exactly this
in the challenges below, where we feed the input for you.)
The one rule that catches everyone
input() always gives you a string β even if the user types a number. So if you try
to do math with it directly, it breaks. Watch:
python
age = "25"print(age + 5)
This example raises an error (on purpose)
TypeError
Your output
That "25" is what input() would have handed you: text, not a number. Adding text and a
number raises TypeError.
The fix: convert with int() and float()
To turn a string into a number, wrap it in int() (whole numbers) or float() (decimals):
python
print(int("25"))
Output
25
Your output
Now the math works:
python
age = int("25")print(age + 5)
Output
30
Your output
For decimals, use float():
python
print(float("3.14"))
Output
3.14
Your output
So the real pattern for reading a number from the user is int(input()) or
float(input()) β read the text, then convert it.
Going the other way: str()
str() converts a number into text, which lets you join it with +:
python
print("Total: " + str(100))
Output
Total: 100
Your output
(Most of the time an f-string is easier β f"Total: {100}" β but str() is good to know.)
Putting it together
Convert the inputs, do the work, then show the result with an f-string:
The challenges below run yourinput() code for real. Write the program, press Run
tests, and weβll feed it sample inputs and check what it prints.
Edge cases worth remembering
int("3.5") raises ValueError β int() wonβt parse a decimal string. Use
float("3.5") instead (then int() on the float if you want to drop the fraction).
int() ignores surrounding spaces (int(" 25 ") is 25) but not stray characters
(int("25kg") raises ValueError).
Check yourself
What type does input() always return?
Reveal answer
A string (str), even if the user types a number β input() always returns a str. If the user types 25, you get the text "25", not the number 25. Convert with int() or float() to do math.
The user typed 25. You wrote age = input(); print(age + 5). What happens?
Reveal answer
TypeError β age is the string "25", and you can't add a string and a number β input() gave the string "25". "25" + 5 mixes text and a number, which raises TypeError. Fix it with age = int(input()).
How do you read a number from the user and use it in math?
Reveal answer
n = int(input()) β Call input() to get the text, then wrap it in int() to convert: int(input()). Use float(input()) for decimals.
What does int("3.5") do?
Reveal answer
Raises ValueError β int() won't parse a decimal string β int() only accepts whole-number strings. For "3.5" use float("3.5") first; int() on that string directly raises ValueError.
Challenges
Challenge 1 +10 XP
Read the user's name with input() and print a greeting. For the name Ada, it must print exactly: Hello, Ada!
python
Test 1 (input: "Ada") β expects "Hello, Ada!"
Test 2 (input: "Sam") β expects "Hello, Sam!"
Need a hint? (β25% XP)
Store input() in a variable, then use an f-string: f"Hello, {name}!"
Show solution (0 XP)
name = input()print(f"Hello, {name}!")
Challenge 2 +15 XP
Read a number with input(), convert it to an integer, and print that number doubled. For input 8, print 16.
python
Test 1 (input: "8") β expects "16"
Test 2 (input: "20") β expects "40"
Need a hint? (β25% XP)
input() gives text, so wrap it: int(input()). Then multiply by 2.