SoFunction
Updated on 2024-12-19

Using the int function in Python

descriptive

The int function converts a numeric string or a decimal number of a specified binary into a shaped form.

grammatical

int(object, base)
name (of a thing) clarification note
object A numeric or string parameter

1. Numeric parameters can be integers, floating-point numbers (both decimal and exponentiated).

2. The string parameter can contain only the characters covered by the specified hexadecimal system.

3. This parameter may be omitted

base binary digit

1. This parameter can be omitted and defaults to 10 when omitted.

2. a positive integer parameter that indicates the object's corresponding system.

give an example

1. Conversion of floating point numbers to integers

test = [12.96, -34.21, 12.0e3]
 
for number in test:
    print(int(number))

The output result is:

12
-34
12000

Note: Regardless of the value of the fractional part of a floating-point number, the int( ) function will only keep the integer part and round off the fractional part when converted. Therefore, you should avoid using the int function directly when rounding floating-point numbers and the like.

2. Conversion of binary numbers to decimal numbers

test = ['111011011111', '0b101']
 
for number in test:
    print(int(number, 2))

The output result is:

3807
5

3. Conversion of octal numbers to decimal numbers

test = ['-1537202', '0o147']
 
for number in test:
    print(int(number, 8))

The output result is:

-441986
103

4. Conversion of hexadecimal numbers to decimal numbers

test = ['34A', '0x17']
 
for number in test:
    print(int(number, 16))

The output result is:

842
23

5. Conversion of boolean values to integers

The simplest datatype in Python is the Boolean, which has only two optional values: True and False, which represent 1 and 0, respectively, when converted to an integer.

>>> int(True)
1
>>> int(False)
0

6. Converting integer strings to integers

You can convert strings containing only numbers and plus or minus signs to integers.

>>> int('99')
99
>>> int('-23')
-23
>>> int('+12')
12

caveat

1. When all parameters are omitted, the integer 0 is returned.

test = int()
print(test, type(test))

The output result is:

0 <class 'int'>

2. Attempting to convert a floating-point string to a decimal integer results in an error:

test = '23.1314'
print(int(test))

The output result is:

Traceback (most recent call last):
  File "/Users/", line 3, in <module>
    print(int(test))
ValueError: invalid literal for int() with base 10: '23.1314'

Returning a value reports an error: for the function int, an invalid literal was used to convert to decimal: 23.1314.

The correct way to use it is to convert a floating-point string to a floating-point type, and then convert the floating-point type to an integer.

test = '23.1314' 
print(int(float(test)))

Return 23.

Note: The int() function can accept floating-point numbers or strings consisting of numbers, but it cannot accept strings containing decimal points or exponents.

3. base parameter error

Python automatically calculates the range of the base parameter. If it is out of range, it will report an error:

test = '110' 
print(int(test, -2))

The output result is:

Traceback (most recent call last):
  File "/Users/", line 3, in <module>
    print(int(test, -2))
ValueError: int() base must be >= 2 and <= 36, or 0

Based on the value of the object parameter, Python automatically calculates the appropriate interval for base.

4. Python reports an error when there are illegal characters in the object parameter.

For example, introducing the character 'A' in an octal numeric character, or the character 'H' in a hexadecimal character

test = '110S' 
print(int(test, 16))

The output result is:

Traceback (most recent call last):
  File "/Users/", line 3, in <module>
    print(int(test, 16))
ValueError: invalid literal for int() with base 16: '110S'

5. The addition of the binary symbol 0b, the octal symbol 0o, and the hexadecimal symbol 0x to the numeric string has no effect on the result and can be omitted.

test_0b = ['0b1011', '1011']
test_0o = ['0o735', '735']
test_0x = ['0xFA', 'FA']
 
for number in test_0b:
    print(int(number, 2))
 
for number in test_0o:
    print(int(number, 8))
 
for number in test_0x:
    print(int(number, 16))

The output result is:

11
11
477
477
250
250

6. Converting a decimal number to a decimal number without reporting any errors will not make any sense.

>>> int(5)
5

It is also possible to convert a decimal integer string to decimal (type conversion)

>>> int('23')
23

7. Conversion of custom hex to decimal

The int function has a powerful customized conversion from hex to decimal. For example, converting a hexadecimal numeric character to a decimal number:

test_17 = 'GG'
print(int(test_17, 17))

The output result is:

288

8. Legitimate numeric character letters are not case-sensitive

For example, in hexadecimal, both A and a can be converted to decimal numbers with the same result.

>>> int('a', 16)
10
>>> int('A', 16)
10

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.