# Python | int() function

int() function in Python and Python3 converts a number in given base to decimal.

Syntax :

`int(string, base)`

Parameter :

```string : consists of 1's and 0's
base : (integer value) base of the number.```

Returns :

```Returns an integer value, which is equivalent
of binary string in the given base. ```

Errors :

```TypeError : Returns TypeError when any data type other
than string or integer is passed in its equivalent position.```

Code #1 :

 `# Python3 program for implementation  ` `# of int() function  ` `num ``=` `13` ` `  `String ``=` `'187'` ` `  `# Stores the result value of ` `# binary "187" and num addition  ` `result_1 ``=` `int``(String) ``+` `num  ` `print``(``"int('187') + 13 = "``, result_1, ``"\n"``) ` ` `  `# Example_2 ` `str` `=` `'100'` ` `  `print``(``"int('100') with base 2 = "``, ``int``(``str``, ``2``)) ` `print``(``"int('100') with base 4 = "``, ``int``(``str``, ``4``)) ` `print``(``"int('100') with base 8 = "``, ``int``(``str``, ``8``)) ` `print``(``"int('100') with base 16 = "``, ``int``(``str``, ``16``)) `

Output :

```int('187') + 13 =  200

int('100') with base 2 =  4
int('100') with base 4 =  16
int('100') with base 8 =  64
int('100') with base 16 =  256```

Code #2 :

 `# Python3 program for implementation  ` `# of int() function  ` ` `  `# "111" taken as the binary string  ` `binaryString ``=` `"111"` ` `  `# Stores the equivalent decimal  ` `# value of binary "111"  ` `Decimal ``=` `int``(binaryString, ``2``)  ` ` `  `print``(``"Decimal equivalent of binary 111 is"``, Decimal)  ` ` `  ` `  `# "101" taken as the binary string ` `binaryString ``=` `"101"` ` `  `# Stores the equivalent decimal  ` `# value of binary "101"  ` `Octal ``=` `int``(binaryString, ``8``)  ` ` `  `print``(``"Octal equivalent of binary 101 is"``, Octal) `

Output :

```Decimal equivalent of binary 111 is 7
Octal equivalent of binary 101 is 65```

Code #3 : Program to demonstrate the TypeError.

 `# Python3 program to demonstrate   ` `# error of int() function  ` ` `  ` `  `# when the binary number is not   ` `# stored in as string  ` `binaryString ``=` `111` ` `  `# it returns an error for passing an   ` `# integer in place of string  ` `decimal ``=` `int``(binaryString, ``2``)  ` ` `  `print``(decimal) `

Output :

```Traceback (most recent call last):
decimal = int(binaryString, 2)
TypeError: int() can't convert non-string with explicit base```

.

Application :
It is used in all the standard conversions. For example conversion of binary to decimal, binary to octal, binary to hexadecimal.

My Personal Notes arrow_drop_up Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.