How to Convert String to Int in Python

If you have text like "123" and want to use it as a number, Python gives you a simple tool: int().

This page shows you:

  • how to convert a string to an integer
  • when int() works
  • how to handle invalid input safely
  • what to do with spaces, signs, and decimal strings

Quick answer

text = "123"
number = int(text)

print(number)
print(type(number))

Output:

123
<class 'int'>

Use int() when the string contains a valid whole number.

What this page helps you do

After reading this page, you should be able to:

  • Convert a numeric string to an integer
  • Understand when int() works
  • Handle bad input safely
  • Know what to do with strings that contain spaces or decimal values

Use int() to convert a string to an integer

The main tool is int().

It converts text like "42" into the number 42.

That result is an int, not a string. This matters because numbers can be used in math, but strings cannot be used the same way.

Example:

text = "42"
number = int(text)

print(number)
print(type(number))

Output:

42
<class 'int'>

If you want a deeper explanation of how this function works, see Python int() function explained.

Basic example

Start with a simple numeric string, pass it to int(), and then use the result in a calculation.

text = "100"
number = int(text)

print(number)
print(type(number))
print(number + 50)

Output:

100
<class 'int'>
150

Why this matters

If you keep the value as a string, Python treats it as text:

text = "100"
print(text + "50")

Output:

10050

But after converting with int(), Python treats it as a number:

text = "100"
number = int(text)

print(number + 50)

Output:

150

Strings with spaces

int() can handle leading and trailing spaces.

This works:

text = "  25  "
number = int(text)

print(number)

Output:

25

You can also clean the string first with strip():

text = "  25  "
clean_text = text.strip()
number = int(clean_text)

print(number)

Output:

25

This is common when reading user input or file data.

If you are checking confusing input, repr() is useful because it shows hidden spaces clearly:

value = "  25  "
print(repr(value))

Output:

'  25  '

Negative numbers and plus signs

Signed whole numbers also work with int().

print(int("-7"))
print(int("+7"))

Output:

-7
7

The string must still be a valid integer.

These will fail:

# int("--7")
# int("+-7")
# int("7-")

This is useful when users may enter positive or negative values.

When conversion fails

int() only works when the string is a valid whole number.

These examples fail:

  • "12.5"
  • "abc"
  • "10a"
  • ""

Example:

print(int("12.5"))

Python raises a ValueError.

A similar error also happens with letters:

print(int("abc"))

When this happens, it usually means the text is not a valid integer.

Common causes:

  • The string contains letters
  • The string contains a decimal point
  • The string is empty
  • The value comes from input() and was not converted
  • The string includes symbols that are not part of a valid integer

If you are seeing a conversion error, see ValueError: invalid literal for int() with base 10.

How to handle invalid input

The safest pattern is to use try and except around int().

This lets your program show a helpful message instead of crashing.

text = "abc"

try:
    number = int(text)
    print("Converted:", number)
except ValueError:
    print("That is not a valid whole number.")

Output:

That is not a valid whole number.

Here is another example with valid input:

text = "45"

try:
    number = int(text)
    print("Converted:", number)
except ValueError:
    print("That is not a valid whole number.")

Output:

Converted: 45

This is especially important for values typed by users.

If you are new to this pattern, read how to use try-except blocks in Python.

Decimal strings are different

A string like "12.5" is not a valid integer string, so this fails:

# int("12.5")

If the text contains a decimal number, use float() first:

text = "12.5"
number = float(text)

print(number)
print(type(number))

Output:

12.5
<class 'float'>

If you need to turn that result into an integer, you can do this:

text = "12.5"
number = int(float(text))

print(number)

Output:

12

Important: this removes the decimal part. It does not round to the nearest whole number.

For more on decimal conversion, see how to convert string to float in Python or Python float() function explained.

Converting user input

input() always returns a string.

So if you expect a whole number, you need to convert it.

age = int(input("Enter your age: "))
print("Next year you will be", age + 1)

If the user types a valid whole number, this works.

But it is safer to add error handling:

text = input("Enter a whole number: ")

try:
    number = int(text)
    print("You entered:", number)
except ValueError:
    print("Please enter a valid whole number.")

This is one of the most common beginner tasks in Python.

If you want to learn more, see Python input() function explained and how to convert user input to numbers in Python.

Common mistakes to avoid

Here are some common beginner mistakes:

  • Trying int() on decimal text like "12.5"
  • Assuming input() returns a number
  • Forgetting to handle ValueError
  • Mixing strings and integers in math

Example of a type mistake:

text = "5"
print(text + 2)

This causes an error because Python cannot add a string and an integer.

Correct version:

text = "5"
number = int(text)

print(number + 2)

Output:

7

If you need to inspect a value before converting it, these checks can help:

value = " 42 "

print(value)
print(type(value))
print(repr(value))
print(value.strip())
print(int(value))

Output:

 42 
<class 'str'>
' 42 '
42
42

These are useful for debugging because they help you see:

  • the actual value
  • its type
  • hidden spaces
  • whether strip() fixes the problem

FAQ

How do I convert a string like "123" to an integer in Python?

Use int("123"). It returns the integer 123.

Why does int("12.5") fail?

Because "12.5" is not a whole-number string. Use float() first if the text contains decimals.

Does input() return an int?

No. input() always returns a string, so you must convert it if you need a number.

How do I stop my program from crashing on bad input?

Wrap int() in a try-except block and catch ValueError.

Can int() handle spaces?

Yes. Leading and trailing spaces are allowed, though strip() can make input cleaner.

See also