How to Convert User Input to Numbers in Python

When you use Python’s input() function, the value you get back is always text.

That means if a user types 25, Python stores it as the string "25", not the number 25.

This page shows you how to:

  • Convert input() values to int
  • Convert input() values to float
  • Understand why input() returns a string
  • Handle bad input without crashing

Quick fix

age = int(input("Enter your age: "))
price = float(input("Enter the price: "))

print(age)
print(price)

Use int() for whole numbers and float() for decimal numbers. input() returns text, so math will not work until you convert it.

What this page helps you do

  • Convert input() values to int
  • Convert input() values to float
  • Understand why input() returns a string
  • Handle bad input without crashing

Why conversion is needed

input() always gives back a string.

Even if the user types something that looks like a number, Python still treats it as text.

For example:

value = input("Enter a number: ")

print(value)
print(type(value))

Possible output:

42
<class 'str'>

This matters because strings and numbers behave differently.

a = input("First number: ")
b = input("Second number: ")

print(a + b)

If the user enters 2 and 3, the output is:

23

That happens because a and b are strings, so + joins the text instead of adding numbers.

Convert input to an integer

Use int(input(...)) when you want a whole number.

This is useful for:

  • Age
  • Count
  • Year
  • Menu choices

Example:

age = int(input("Enter your age: "))
print(age)
print(type(age))

Possible output:

25
<class 'int'>

A few important points:

  • Use int() for whole numbers only
  • int("10") works
  • int("3.5") does not work
  • Your prompt should clearly ask for a whole number

Example with a clear prompt:

year = int(input("Enter a whole-number year: "))
print("Next year will be:", year + 1)

Convert input to a float

Use float(input(...)) when decimal numbers are allowed.

This is useful for:

  • Price
  • Height
  • Temperature
  • Weight

Example:

price = float(input("Enter the price: "))
print(price)
print(type(price))

Possible output:

19.99
<class 'float'>

A few things to remember:

  • float("3.5") works
  • float("5") also works
  • The result is always a floating-point number

Example:

height = float(input("Enter your height in meters: "))
print("Your height is", height)

Do math after converting

Convert first, then do the math.

If you need two numbers, use two separate input() calls.

num1 = int(input("Enter the first whole number: "))
num2 = int(input("Enter the second whole number: "))

total = num1 + num2

print("Total:", total)

Possible output if the user enters 4 and 6:

Total: 10

For decimal values, use float() instead:

price1 = float(input("Enter the first price: "))
price2 = float(input("Enter the second price: "))

total = price1 + price2

print("Total price:", total)

Printing the result is a simple way to confirm that the conversion worked.

Handle invalid input safely

If the user types letters or the wrong kind of number, conversion can fail.

For example:

age = int(input("Enter your age: "))

If the user types hello, Python raises a ValueError.

To prevent your program from crashing, use a try-except block. If you are new to this, see how to use try-except blocks in Python.

Example: safe integer input

try:
    age = int(input("Enter a whole-number age: "))
    print("Your age is:", age)
except ValueError:
    print("Please enter a valid whole number.")

Example: ask again until the input is valid

while True:
    try:
        price = float(input("Enter a price: "))
        print("You entered:", price)
        break
    except ValueError:
        print("Please enter a valid number, such as 10 or 10.5.")

This is better than letting the program crash, because it tells the user what went wrong and gives them another chance.

If you get a conversion error often, read ValueError: invalid literal for int() with base 10.

Choose int or float

Use int when you expect whole numbers only.

Use float when decimal values are allowed.

Quick guide:

  • Use int for values like 1, 25, or 300
  • Use float for values like 3.14, 9.5, or 12.0
  • If decimals must be kept, do not convert to int

Examples:

count = int(input("How many books? "))
temperature = float(input("Enter the temperature: "))

If you are unsure, think about the real data you expect from the user.

  • Number of people → int
  • Product price → float
  • Year → int
  • Height → float

Beginner tips

  • Use clear prompts like Enter a whole number:
  • Remember that spaces around numbers are usually handled
  • Test with both valid and invalid input
  • Check the error message if conversion fails

For example, this usually works because Python ignores extra spaces around the text:

number = int(input("Enter a whole number: "))
print(number)

If the user types:

   7

Python can still convert it correctly.

Useful debugging steps:

print(value)
print(type(value))
number = int(value)
number = float(value)

These can help you check whether a value is still a string before you use it in math.

Common mistakes

Here are some common causes of problems when converting user input:

  • Trying to add two input() values without converting them
  • Using int() on decimal text like 4.2
  • Typing letters or symbols when a number is expected
  • Assuming input() returns a number automatically
  • Using the converted value in math before checking for errors

Example of a common mistake:

a = input("Enter a number: ")
b = input("Enter another number: ")

print(a + b)

If the user enters 4 and 2, the result is:

42

Correct version:

a = int(input("Enter a number: "))
b = int(input("Enter another number: "))

print(a + b)

FAQ

Why does input() not return a number?

input() reads what the user types as text, so it returns a string.

When should I use int()?

Use int() when you only want whole numbers like 1, 25, or 300.

When should I use float()?

Use float() when decimal numbers are allowed, like 3.14 or 9.5.

What error happens if conversion fails?

Python raises ValueError when text cannot be turned into the number type you asked for.

Can int() convert 4.0 from user input?

No. int("4.0") raises ValueError because it is decimal text. Use float() first if needed.

See also