int() in Python is a built-in function that returns the corresponding integer object of passed number or string as a parameter.

Syntax:

int() Parameters

object [optional] – Any object like integer, float, string, etc that need to be converted to integer.
base [optional] – Base value for the conversion.

If no argument is passed then int() returns 0 as a result.

base is required only for string object passed as the first argument. For numbers, the default base value is 10.

Python int() Examples

Example 1: passing Integers

Example 2: passing Floating Numbers

Example 3: passing String

If base not specified, by default it would be 10.

Example 4: passing Binary

To pass binary, prefix it with 0b or 0B. Do not pass any argument for base otherwise it will throw error because base is only used with string object.

Example 5: passing Octal

To pass Octal prefix it with 0c or 0C.

Example 5: passing Hexadecimal

To pass Hexadecimal prefix it with 0x or 0X.

So, use int() to convert any numbers or string into an integer if possible in Python.

If you have any doubts or suggestion then comment below.

Leave a Reply

Close Menu