# Python int() Function

This is a tutorial of the Python built-in int() Function. Learn to convert a given object such as a number or string into an integer with this method.

### Python `int()` Function

As suggested by the name of the function itself, it converts a given string object or a number into an integer value and returns it.

It is among the most commonly used Python built-in functions. One of the common usages of this function is while taking input from the user in the console. Input is always taken as a string and hence using this function we can convert the string type input into a valid integer value.

### Syntax

Given below is the syntax of the `int()` function.

### `int()` Parameters

This function can take up to two different arguments. These are described below.

• object. This is a string or number for which you want to find out the corresponding integer value. The default value of this parameter is set to 0. This should be a valid object that could be converted into an integer.
• base. This is the base of the number passed as the first argument. As you might be knowing that the base of all of the decimal numbers is 10, that’s why the default value of this parameter is also set to 10. Hence, the integer function considers the number as the decimal number, if you only pass the number as the argument and do not define a custom base. The valid value of the base could be 0 or 2-36.

### `int()` Return Value

• It returns an integer object. This object is the integer number that is being formed after doing the conversion from a valid number string or another number.
• If no argument is passed to `int()`, it returns 0 because the default value of the first argument is 0 and also the default base is 10. The integer value for the decimal number is 0 is 0.
• Returns the decimal value for the string-based numbers in the given base. For example, we can convert return the decimal number for an octal number (base 8) or a number in any other base (2-36) to decimal number.

Related Article. Octal To Decimal Conversion In Python

## Examples

Three different examples are given below illustrating the different use-cases of the Python built-in `int()` function.

### Example 1. Basic usage of the `int()` function

Here we’re using the function `int()` to convert integer, floating-point number, and a string containing a valid number into the int data type.

```#Converting Integer into Integer
print("int(525) gives :", int(525))
print("Data Type of int(35) is:", type(int(35)))

#Converting Float To Integer
print("int(56.89) gives :", int(56.89))

#Converting String To Integer
print("int('8450') gives:", int('8450'))```

Output.

### Example 2. Converting Octal, Hexadecimal & Other Base Numbers into Decimal Numbers using `int()`

You can also make use of the `int()` function to convert a number in any base from 2 to 36 into decimal numbers. In the following code, we’re converting the Binary numbers (Base 2), Octal Numbers (Base 8) and Hexadecimal Numbers (Base 16) into Decimal Numbers.

```#Converting Binary Numbers into Decimal Numbers
print("Binary: 10110", "Integer:", int('10110', 2))
print("Binary: 0b101110", "Integer:", int('0b101110', 2))

#Converting Octal Numbers into Decimal Numbers
print("Octal: 15", "Integer:", int('15', 8))
print("Octal: 0o14", "Integer:", int('0o14', 8))

#Converting Hexadecimal Numbers into Decimal Numbers

Output.

### Example 3. Working of the function `int()` for the custom class objects

Whenever you apply the `int()` function for a given object, it internally calls the class method `__int__()` for that particular object. Therefore, if you want this function to return a valid integer number for a custom class object, then you can override the internal class methods, namely `__index__()` and `__int__()` to make this function return a valid integer number for them as well.

The reason why you need  to make both of these methods `__index__()` and `__int__()` to return the same integer number is that the older versions of the python use the `__int__()` function while the new versions of python use the `__index__()` method for the same purpose.

```class Mango:
quantity = 100

def __index__(this):
return this.quantity

def __int__(this):
return this.quantity

obj = Mango()
print("int(obj) gives:", int(obj))```

Output.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!