Python int() Function Explained

The Python int() function converts a value into an integer.

Beginners often use int() when they want to:

  • turn text like "42" into the number 42
  • convert user input into a whole number
  • remove the decimal part from a float
  • read numbers written in binary or hexadecimal

It is a built-in function, so you can use it directly without importing anything.

Quick example #

print(int("42"))
print(int(3.9))
print(int("101", 2))

Output:

42
3
5

Use int() to convert a number-like value to an integer. Strings must contain a valid whole number unless you also provide a base.

What int() does #

int() converts a value into an integer.

Key points:

  • It can turn strings, floats, and booleans into int values.
  • It returns a new integer value.
  • It does not change the original object.

Example:

text = "25"
number = int(text)

print(text)
print(number)
print(type(text))
print(type(number))

Output:

25
25
<class 'str'>
<class 'int'>

The original string stays a string. int() creates a new integer from it.

Basic syntax #

int(x)
int(x, base)

int(x) #

Use this form when you want to convert a value like:

  • a string such as "10"
  • a float such as 3.9
  • a boolean such as True

Example:

print(int("10"))
print(int(8.7))
print(int(True))

Output:

10
8
1

int(x, base) #

Use this form when x is a string that represents a number in another base.

Common bases:

  • 2 for binary
  • 8 for octal
  • 16 for hexadecimal

Example:

print(int("101", 2))
print(int("17", 8))
print(int("1A", 16))

Output:

5
15
26

If you want to compare integer conversion with decimal conversion, see the float() function.

Using int() with strings #

int() works with strings that contain a valid integer.

Examples that work:

print(int("5"))
print(int("-12"))
print(int("  42  "))

Output:

5
-12
42

This works because each string contains valid whole-number text. Leading and trailing spaces are usually allowed.

Examples that fail:

# print(int("3.14"))
# print(int("12abc"))

These raise ValueError because the strings are not valid integer text.

Valid string examples #

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

Output:

0
7
-100

Invalid string examples #

values = ["3.14", "12abc", "", "seven"]

for value in values:
    try:
        print(int(value))
    except ValueError as error:
        print(f"{value!r} -> {error}")

Output:

'3.14' -> invalid literal for int() with base 10: '3.14'
'12abc' -> invalid literal for int() with base 10: '12abc'
'' -> invalid literal for int() with base 10: ''
'seven' -> invalid literal for int() with base 10: 'seven'

If you are working with user input, see how to convert user input to numbers in Python and the input() function.

Using int() with floats #

When you pass a float to int(), Python removes the decimal part.

Important: int() does not round. It truncates toward zero.

print(int(3.9))
print(int(3.1))
print(int(-3.9))
print(int(-3.1))

Output:

3
3
-3
-3

This behavior matters with negative numbers.

  • int(3.9) becomes 3
  • int(-3.9) becomes -3

If you want decimal conversion instead, use float(). If you want text conversion, use str().

Using int() with booleans #

Booleans can also be converted to integers.

print(int(True))
print(int(False))

Output:

1
0

This can be useful in simple counting logic:

passed = True
total = int(passed)

print(total)

Output:

1

If you want to learn more about boolean values, see the bool() function.

Using the base argument #

Use the base argument when a string represents a number in a different number system.

Binary example #

print(int("101", 2))

Output:

5

"101" in binary means:

  • 1 × 4
  • 0 × 2
  • 1 × 1

So the result is 5.

Hexadecimal example #

print(int("1A", 16))

Output:

26

Important rule #

The base argument is for string-like input, not normal numeric values.

Good:

print(int("101", 2))

Not correct:

# int(101, 2)

That causes a TypeError because base should not be used with a normal integer value.

What errors can happen #

Two common errors with int() are ValueError and TypeError.

ValueError #

This happens when a string is not a valid integer.

Example:

try:
    print(int("3.14"))
except ValueError as error:
    print(error)

Output:

invalid literal for int() with base 10: '3.14'

A decimal string like "3.14" does not work directly with int(). You need to convert it to a float first:

print(int(float("3.14")))

Output:

3

If you see this often, read how to convert string to int in Python and how to fix ValueError: invalid literal for int() with base 10.

TypeError #

This can happen with unsupported input types.

Example:

try:
    print(int(None))
except TypeError as error:
    print(error)

Output:

int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

Another example is using the base argument with a non-string numeric value:

try:
    print(int(101, 2))
except TypeError as error:
    print(error)

Output:

int() can't convert non-string with explicit base

When to use int() #

Use int() when you need a whole number.

Common uses:

  • Convert user input from input() into integers
  • Turn float results into integers when truncation is acceptable
  • Parse numeric strings from files or APIs
  • Convert binary or hexadecimal strings with the base argument

Example with user input:

age_text = "18"
age = int(age_text)

print(age + 1)

Output:

19

Common mistakes #

Here are some common problems beginners run into with int().

Passing a string with decimal text #

This fails:

# int("4.5")

Why: "4.5" is not an integer string.

Use this instead:

print(int(float("4.5")))

Passing text with letters #

This fails:

# int("12abc")

Why: the string contains non-numeric characters.

Trying int(None) #

This fails because None is not a number or numeric string.

value = None

try:
    print(int(value))
except TypeError as error:
    print(error)

Using the base argument with a non-string #

This fails:

# int(101, 2)

Use a string instead:

print(int("101", 2))

Expecting int() to round #

This is a very common misunderstanding.

print(int(4.9))

Output:

4

If you expected 5, remember that int() truncates. It does not round.

FAQ #

Does int() round numbers? #

No. int() removes the decimal part. It does not round to the nearest whole number.

Why does int("3.14") fail? #

Because "3.14" is not an integer string. Convert it with float() first, then use int() if needed.

What does int(True) return? #

It returns 1. False becomes 0.

When should I use the base argument? #

Use it when the input string is written in another base, such as binary or hexadecimal.

See also #

Press Esc to close