Python3 Data Type Conversion
Sometimes we need to convert built-in data types; in general you only need to use the data type as a function name.
Python data type conversion can be divided into two kinds:
- Implicit type conversion - done automatically
- Explicit type conversion - requires type functions to convert
Implicit type conversion
In implicit type conversion, Python automatically converts one data type to another without our intervention.
In the following example we perform an operation on data of two different types; the lower data type (integer) is converted to the higher data type (floating-point) to avoid data loss.
Example
num_int
=
123
num_flo
=
1.23
num_new
=
num_int + num_flo
(
"The data type of num_int is:"
,
type
(
num_int
)
)
(
"The data type of num_flo is:"
,
type
(
num_flo
)
)
(
"The value of num_new is:"
,
num_new
)
(
"The data type of num_new is:"
,
type
(
num_new
)
)
The output of the above example is:
num_int 数据类型为:
num_flo 数据类型为:
num_new: 值为: 124.23
num_new 数据类型为:
Code analysis:
- In the example we add two variables of different data types
num_intandnum_floand store the result in the variablenum_newit. - Then check the data types of the three variables.
- In the output we see
num_intIs整型(integer),num_floIs浮点型(float)。 - Likewise, the new variable
num_newIs浮点型(float), because Python converts the smaller data type to the larger data type to avoid data loss.
Let's look at another example, adding integer data and string data:
Example
num_int
=
123
num_str
=
"456"
(
"The data type of num_int is:"
,
type
(
num_int
)
)
(
"The data type of num_str is:"
,
type
(
num_str
)
)
(
num_int+num_str
)
The output of the above example is:
num_int 数据类型为:
num_str 数据类型为:
Traceback (most recent call last):
File "/runoob-test/test.py", line 7, in
print(num_int+num_str)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
As can be seen from the output, an operation between an integer and a string type raises an error, outputting TypeError. Python cannot use implicit conversion in this case.
However, Python provides a solution for these type situations, called explicit conversion.
Explicit type conversion
In explicit type conversion, the user converts an object's data type to the desired data type. We use predefined functions such as int(), float() and str() to perform explicit type conversion.
int() forces conversion to an integer:
Example
x
=
int
(
1
)
x outputs 1
y
=
int
(
2.8
)
y outputs 2
z
=
int
(
"3"
)
z outputs 3
float() forces conversion to a floating-point number:
Example
x
=
float
(
1
)
x outputs 1.0
y
=
float
(
2.8
)
y outputs 2.8
z
=
float
(
"3"
)
z outputs 3.0
w
=
float
(
"4.2"
)
w outputs 4.2
str() forces conversion to a string type:
Example
x
=
str
(
"s1"
)
x outputs 's1'
y
=
str
(
2
)
y outputs '2'
z
=
str
(
3.0
)
z outputs '3.0'
Operations between integers and strings can be done using forced type conversion:
Example
num_int
=
123
num_str
=
"456"
(
"The data type of num_int is:"
,
type
(
num_int
)
)
(
"Before type conversion, the data type of num_str is:"
,
type
(
num_str
)
)
num_str
=
int
(
num_str
)
Force conversion to an integer
(
"After type conversion, the data type of num_str is:"
,
type
(
num_str
)
)
num_sum
=
num_int + num_str
(
"The result of adding num_int and num_str is:"
,
num_sum
)
(
"The data type of sum is:"
,
type
(
num_sum
)
)
The output of the above example is:
num_int 数据类型为:
类型转换前,num_str 数据类型为:
类型转换后,num_str 数据类型为:
num_int 与 num_str 相加结果为: 579
sum 数据类型为:
The following built-in functions can perform conversions between data types. These functions return a new object representing the converted value.
| function | Description |
|---|---|
|
Convert x to an integer |
|
|
Convert x to a floating-point number |
|
|
Create a complex number |
|
|
Convert object x to a string |
|
|
Convert object x to an expression string |
|
|
Used to evaluate a valid Python expression in a string and return an object |
|
|
Convert sequence s to a tuple |
|
|
Convert sequence s to a list |
|
|
Convert to a mutable set |
|
|
Create a dictionary. d must be a sequence of (key, value) tuples. |
|
|
Convert to an immutable set |
|
|
Convert an integer to a character |
|
|
Convert a character to its integer value |
|
|
Convert an integer to a hexadecimal string |
|
|
Convert an integer to an octal string |
|
|
Convert object x to a Boolean value (True or False) |
|
|
Convert an object to an immutable byte sequence |
|
|
Convert an object to a mutable byte array |
|
|
Return a memory view object of the given argument (without copying data) |
|
|
Convert an integer to a binary string |
|
|
Return the ASCII representation of an object, with non-ASCII characters escaped |