GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask
  • Introduction1 min read
  • Input & Output2 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Python int() Function - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Python Tutorial
  • Data Types
  • Interview Questions
  • Examples
  • Quizzes
  • DSA Python
  • Data Science
  • NumPy
  • Pandas
  • Practice
  • Django
  • Flask

Python int() Function

Last Updated : 26 Sep, 2025

The Python int() function converts a given object to an integer or converts a decimal (floating-point) number to its integer part by truncating the fractional part.

Example: In this example, we passed a string as an argument to the int() function and printed it.

Python
age = "21"
print("age =", int(age))

Output:

age = 21

Python int() Function Syntax

The syntax of the int() function in Python is as follows:

Syntax: int(x, base)

  • x [optional]: string representation of integer value, defaults to 0, if no value provided.
  • base [optional]: (integer value) base of the number.

Returns: Return decimal (base-10) representation of x

Python int() Function Examples

Let us see a few examples of int() in Python.

Python int() with different DataTypes

In this example, we passed a string, a float, and an integer value as a parameter to the int() function in Python to see how the input is affected by applying the int() function.

Python
# int() on string representation of numbers
print("int('9')) =", int('9'))

# int() on float values
print("int(9.9) =", int(9.9))

# int() on Python integer
print("int(9) =", int(9))

Output : 

int('9')) = 9
int(9.9) = 9
int(9) = 9

Convert base using int() in Python

In this example, we used the int() function to convert the base of a number from Binary, Octal, and Hexadecimal to a Decimal integer.

Python
# octal to decimal using int()
print("int() on 0o12 =", int('0o12', 8))

# binary to decimal using int()
print("int() on 0b110 =", int('0b110', 2))

# hexa-decimal to decimal using int()
print("int() on 0x1A =", int('0x1A', 16))

Output : 

int() on 0o12 = 10
int() on 0b110 = 6
int() on 0x1A = 26

Exception of int() in Python

TypeError: raises TypeError when any object that does not have __int__() or __index__() Python magic methods implemented.

ValueError: raises ValueError when any object cannot be converted to an integer.

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

In this example, we are trying to convert a binary number to a decimal number using the Python int() function. But this code will raise an error as the binary number passed is not in a single quote.

Python
print(int(0b101, 2))

Output: 

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

ValueError: invalid literal for int() with base 10: 'geeks'

This example will generate a value error as we are passing a Python String to the int() function.

Python
print(int('geeks'))

Output:

ValueError: invalid literal for int() with base 10: 'geeks'

Python int() Function for Custom Objects

The int() in Python can be used on custom objects. Let us see a few examples of the same.

int() with __int__() function

In this example, we created a class called Number and defined a __int__() method that returns the integer value. Then we created an object called data of the Number class. When we pass this object to the int() function, Python internally invokes the __int__() method, and the returned value is used as the integer representation.

Python
class Number:
    value = 7
    
    def __int__(self):
        return self.value

data = Number()
print("number =", int(data))

Output:

number = 7

int() with __index__() function

In this example, we created a class called 'Number' and defined a value and a function that returns the value. Then we created an object called 'data' of the Number class which invokes the __index__() method. The data object stores the value that is returned by that method and then using the int() function we printed the value.

Python
class Number:
    value = 7
    
    def __index__(self):
        return self.value

data = Number()
print("number =", int(data))

Output:

number = 7

Comment

Explore

    Python Fundamentals
    • Introduction1 min read
    • Input & Output2 min read
    • Variables4 min read
    • Operators4 min read
    • Keywords2 min read
    • Data Types4 min read
    • Conditional Statements3 min read
    • Loops3 min read
    • Functions4 min read
    Python Data Structures
    • String4 min read
    • List3 min read
    • Tuples3 min read
    • Dictionary3 min read
    • Set2 min read
    • Arrays5 min read
    Advanced Python
    • OOP Concepts3 min read
    • Exception Handling5 min read
    • File Handling4 min read
    • Python Database2 min read
    • MongoDB3 min read
    • MySQL7 min read
    • Packages4 min read
    • Modules3 min read
    • DSA Libraries5 min read
    • Python GUI2 min read
    Data Science with Python
    • Numpy3 min read
    • Pandas4 min read
    • Matplotlib3 min read
    • Seaborn3 min read
    • StatsModel2 min read
    • Model Building6 min read
    • TensorFlow2 min read
    • PyTorch5 min read
    Web Development with Python
    • Flask3 min read
    • Django5 min read
    • Django ORM4 min read
    • Jinja2 Templating5 min read
    • Django Templates5 min read
    • REST API3 min read
    • Build API with DRF4 min read
    Python Practice
    • Quiz1 min read
    • Practice Problems2 min read
    • Interview Q & A15+ min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • AI Engg Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.