Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python int() Function

Python int() Function

Python3 Built-in Functions Python3 built-in function


int() is one of the most commonly used built-in Python functions, converting data of other types to an integer.

In programming, we often need to convert strings, floats, or other types of data to integers for numeric calculations or comparisons.int() The function helps us accomplish this conversion easily.

Word meaning: int Is integeris short for (integer), denoting the integer type.


Basic syntax and parameters

int() is a built-in function that can be called directly without importing any module.

Syntax format


int(x)
int(x, base)

Parameter Description

- Type: string, number, or another object convertible to an integer - Description: the value to convert to an integer. If x is a string, the base can be specified with the base parameter. - Type: integer (2, 8, 10, 16) - Description: the base of x, defaulting to 10 (decimal). Only valid when x is a string.

Function description

- `int()` returns 0 with no arguments - Floats are truncated toward zero - The string must be a valid numeric representation

Example

Let us thoroughly master int() through a series of examples from simple to complex.

Example 1: basic usage - converting numbers

Example

Convert from a float

print

(

int

(

3.7

)

)

     

Output: 3

print

(

int

(

3.7

)

)

   

Output: -3

Convert from a boolean

print

(

int

(

True

)

)

   

Output: 1

print

(

int

(

False

)

)

   

Output: 0

Convert from a string

print

(

int

(

"42"

)

)

   

Output: 42

print

(

int

(

"  10  "

)

)

Output: 10 (whitespace removed automatically)

Expected output:

3
-3
1
0
42
10

Code analysis:

  1. When converting a float to an integer, the decimal part is truncated directly (toward zero) rather than rounded.
  2. Boolean True converts to 1,False converts to 0.
  3. When converting a string, leading and trailing whitespace is stripped automatically.

Example 2: base conversion

int() Supports converting strings in different bases to decimal integers.

Example

Binary string (starting with 0b)

print

(

int

(

"1010"

,

2

)

)

 

Output: 10

Octal string (starting with 0o)

print

(

int

(

"12"

,

8

)

)

   

Output: 10

Hexadecimal string (starting with 0x)

print

(

int

(

"a"

,

16

)

)

   

Output: 10

print

(

int

(

"FF"

,

16

)

)

   

Output: 255

Strings with a prefix

print

(

int

(

"0b1010"

,

2

)

)

Output: 10

print

(

int

(

"0o12"

,

8

)

)

 

Output: 10

print

(

int

(

"0xff"

,

16

)

)

 

Output: 255

Expected output:

10
10
10
255
10
10
255

Code analysis:

Example 3: common application scenarios

int() There are many application scenarios in real programming, such as handling user input and cleaning data.

Example

Handle user input (input returns a string)

Suppose the user entered "100"

user_input

=

"100"

price

=

int

(

user_input

)

print

(

f

"Product price: {price} yuan"

)

 

Output: Product price: 100 yuan

A numeric string read from a file

numbers_str

=

[

"10"

,

"20"

,

"30"

]

numbers

=

[

int

(

n

)

for

n

in

numbers_str

]

print

(

numbers

)

 

Output: [10, 20, 30]

Calculate the total

total

=

sum

(

numbers

)

print

(

f

"Total: {total}"

)

 

Output: Total: 60

Handle strings with decimals (convert to float first, then to int)

price_str

=

"19.99"

price

=

int

(

float

(

price_str

)

)

print

(

f

"Price (rounded down): {price} yuan"

)

 

Output: Price (rounded down): 19 yuan

Expected output:

商品价格: 100 元
[10, 20, 30]
总和: 60
价格(取整): 19 元

This example shows int() common uses in real applications, including handling user input and converting data in bulk.


Python3 Built-in Functions Python3 built-in function