Python int() 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
- Parameter x:
- 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.
- Parameter base(optional):
- Type: integer (2, 8, 10, 16)
- Description: the base of x, defaulting to 10 (decimal). Only valid when x is a string.
Function description
- Return Value: returns an integer object.
- Special cases:
int()Returns 0 with no arguments- Floats are rounded down (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
(
int
(
3.7
)
)
Output: 3
(
int
(
3.7
)
)
Output: -3
Convert from a boolean
(
int
(
True
)
)
Output: 1
(
int
(
False
)
)
Output: 0
Convert from a string
(
int
(
"42"
)
)
Output: 42
(
int
(
" 10 "
)
)
Output: 10 (whitespace removed automatically)
Expected output:
3
-3
1
0
42
10
Code analysis:
- When converting a float to an integer, the decimal part is truncated directly (toward zero) rather than rounded.
- Boolean
Trueconverts to 1,Falseconverts to 0. - 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)
(
int
(
"1010"
,
2
)
)
Output: 10
Octal string (starting with 0o)
(
int
(
"12"
,
8
)
)
Output: 10
Hexadecimal string (starting with 0x)
(
int
(
"a"
,
16
)
)
Output: 10
(
int
(
"FF"
,
16
)
)
Output: 255
Strings with a prefix
(
int
(
"0b1010"
,
2
)
)
Output: 10
(
int
(
"0o12"
,
8
)
)
Output: 10
(
int
(
"0xff"
,
16
)
)
Output: 255
Expected output:
10
10
10
255
10
10
255
Code analysis:
- The second argument specifies the numeric base of the string: 2 means binary, 8 octal, and 16 hexadecimal.
- A string may have a prefix (such as
0b,0o,0x) or not. - In hexadecimal, a-f may be uppercase or lowercase.
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
)
(
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
]
(
numbers
)
Output: [10, 20, 30]
Calculate the total
total
=
sum
(
numbers
)
(
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
)
)
(
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 function