summary
Data type conversion refers to the conversion of one data type from the original type to another through some method. For example, we convert the string "123" to the number 123, which is a data type conversion.
Python supports the conversion between various standard data types, but not any data can be converted. All conversions should comply with "common sense" and should be logically valid. For example, you shouldn't try to convert a complex type to int, because Python doesn't know how to convert it.
Summary of data type conversion support
Int | Float | Bool | Complex | String | Bytes | List | Tuple | Set | Dict | |
---|---|---|---|---|---|---|---|---|---|---|
Int | - | Y | Y | N | Y | Y | N | N | N | N |
Float | Y | - | Y | N | Y | Y | N | N | N | N |
Bool | Y | Y | - | Y | Y | Y | Y | Y | Y | Y |
Complex | Y | Y | Y | - | Y | N | N | N | N | N |
String | Y | Y | Y | Y | - | Y | Y | Y | Y | Y |
Bytes | Y | N | Y | N | Y | - | Y | Y | Y | N |
List | N | N | N | N | Y | Y | - | Y | Y | Y |
Tuple | N | N | N | N | Y | Y | Y | - | Y | Y |
Set | N | N | N | N | Y | Y | Y | Y | - | Y |
Dict | N | N | N | N | Y | N | Y | Y | Y | - |
Note: Bytes only considers direct conversion
Conversion instance
Convert to int
print(int(1.2)) # float -> int print(int('123')) # string -> int print(int(b'456')) # bytes -> int print('0x%x' % (int.from_bytes(b'456', byteorder='little', signed=True))) print(int(True)) # bool -> int
Convert to float
print(float('1.2')) # string->float print(float(b'3.4')) # bytes -> float print(float(123)) # int->float print(float(False)) # bool->float
Convert to bool
# All types can be converted to bool print(bool(1)) # int->bool print(bool(0.0)) # float->bool print(bool(0 + 0j)) # complex->bool print(bool('')) # String - > bool, empty string is False, others are True print(bool(b'hello')) # Bytes - > bool, null is False, others are True print(bool.from_bytes(b'\x00', byteorder='little')) # bytes->bool print(bool([])) # List - > bool, null is False, others are True print(bool(())) # Tuple - > bool, null is False, others are True print(bool({})) # Dict - > bool, null is False, others are True print(bool(set())) # Set - > bool, null is False, others are True
Convert to complex
print(complex(100)) # int->complex print(complex(1.2)) # float->complex print(complex(True)) # bool->complex print(complex('1.2+2.3j')) # string->complex
Convert to string
# All basic types can be converted to string print(b'hello'.decode('utf-8')) # bytes->string print(str(1)) # int->string print(str(1.2)) # float->string print(str(True)) # bool->string print(str(1.2 + 2.3j)) # Complex - > string all others are True print(str(['hello', 100])) # list->string print(str(('hello', 100))) # tuple->string print(str({'name': 'xiaowang', 'age': 20})) # dict->string print(str({'name', 'age'})) # set->string
Convert to bytes
# Because all types can be converted to string and string can be converted to bytes, all types can be indirectly converted to bytes. # Next, we will only discuss the type directly converted to bytes print('bytes'.center(30, '*')) print(b'\x64') # int to bytes print(int.to_bytes(100, byteorder='big', signed=True, length=2)) # int to bytes print(bool.to_bytes(True, byteorder='big', signed=True, length=2)) # bool to bytes print('hello'.encode(encoding='utf-8')) # string to bytes print(bytes([1, 200, 80, 50])) # list to bytes print(bytes((1, 200, 80, 50))) # tuple to bytes print(bytes({1, 200, 80, 50})) # set to bytes
Convert to list
print(list("hello")) # string->list print(list(b'hello')) # bytes->list print(list((100, 200, 300))) # tuple->list print(list({'name', 'age'})) # set->list print(list({'name': 'xiaowang', 'age': 20})) # Dict - > list, only the key value is taken
Convert to tuple
print(tuple("hello")) # string->tuple print(tuple(b"hello")) # bytes->tuple print(tuple([100, 200, 300])) # list->tuple print(tuple({'name', 'age'})) # set->tuple print(tuple({'name': 'xiaowang', 'age': 20})) # Dict - > tuple, only the key value is taken
Convert to set
print(set("hello")) # string->set print(set(b"hello")) # bytes->set print(set([100, 200, 300])) # list->set # print(set([100, 200, [300, 400]])) # List - > set, the list contains variable data types, and an exception is reported print(set(('name', 'age'))) # tuple->set # print(set(('name', 'age', []))) # Tuple - > set, including variable data types, and an exception is reported print(set({'name': 'xiaowang', 'age': 20})) # Dict - > set, only the key value is taken
Convert to dict
1,string->dict
# Method 1: use json conversion. The string format needs to be strictly in accordance with the json format user_str = '{"name": "xiaowang", "city": "Chengdu", "age": 28}' import json print(json.loads(user_str)) # Mode 2. Use eval function conversion. eval has potential safety hazards and is not recommended print(eval(user_str)) # Method 3: use ast.literal_eval import ast print(ast.literal_eval(user_str))
2,list->dict
# Method 1. zip is required user_keys = ['name', 'city', 'age'] user_values = ['xiaowang', 'Chengdu', 28] print(dict(zip(user_keys, user_values))) # Mode 2: 2D list user_info = [ ["name", "xiaowang"], ["city", "Chengdu"], ["age", 28] ] print(dict(user_info))
Set - > dict tuple - > dict is the same as list - > dict
bytearray ⇋ hex
# hex_str-->bytearray byte_array = bytearray.fromhex("050460000008d40462000007670463") print(byte_array) # bytearray-->hex_str hex_str = byte_array.hex() print(hex_str)
Space separated hex:
# hex_str-->bytearray byte_array = bytearray.fromhex("05 04 60 00 00 08 d4 04 62 00 00 07 67 04 63") print(byte_array) # bytearray-->hex_str hex_str = byte_array.hex() hex_str_space = " ".join([hex_str[i - 1:i + 1] if i % 2 else "" for i in range(len(hex_str))]).strip() print(hex_str_space)
bytearray ⇋ int
import struct # int-->bytearray bytearray_short = struct.pack("<h",666) print(bytearray_short) bytearray_int = struct.pack("<i",666) print(bytearray_int) bytearray_long = struct.pack("<l",666) print(bytearray_long) # bytearray-->int int_short = struct.unpack("<h",bytearray_short)[0] print(int_short) int_int = struct.unpack("<i",bytearray_int)[0] print(int_int) int_long = struct.unpack("<l",bytearray_long)[0] print(int_long)
bytearray ⇋ str
# str-->bytearray byte_array = bytearray("liuyang", encoding='utf-8') print(byte_array) # bytearray-->str st_r = byte_array.decode('utf-8') print(st_r)
appendix
character | Byte order | size | Alignment |
---|---|---|---|
@ | By original byte | By original byte | By original byte |
= | By original byte | standard | nothing |
< | Small end | standard | nothing |
> | Big end | standard | nothing |
! | Network (= big end) | standard | nothing |
format | Type C | Python type | Standard size | notes |
---|---|---|---|---|
x | Fill byte | nothing | ||
c | char | Byte string with length of 1 | 1 | |
b | signed char | integer | 1 | (1), (2) |
B | unsigned char | integer | 1 | (2) |
? | _Bool | bool | 1 | (1) |
h | short | integer | 2 | (2) |
H | unsigned short | integer | 2 | (2) |
i | int | integer | 4 | (2) |
I | unsigned int | integer | 4 | (2) |
l | long | integer | 4 | (2) |
L | unsigned long | integer | 4 | (2) |
q | long long | integer | 8 | (2) |
Q | unsigned long long | integer | 8 | (2) |
n | ssize_t | integer | (3) | |
N | size_t | integer | (3) | |
e | (6) | Floating point number | 2 | (4) |
f | float | Floating point number | 4 | (4) |
d | double | Floating point number | 8 | (4) |
s | char[] | Byte string | ||
p | char[] | Byte string | ||
P | void * | integer | (5) |
-
'?' The conversion code corresponds to the conversion code defined by C99_ Bool type. If this type is not available, use char to simulate. In standard mode, it is always represented in one byte.
-
When attempting to package a non integer with any integer conversion code, if the non integer has __index__() Method calls the method before packing and converts the parameter to an integer.
Change in version 3.2: added use for non integer __index__() Method.
-
The 'N' and 'N' conversion codes are only available for native size (select as default or use '@' byte order characters). For standard sizes, you can use any other integer format suitable for your application.
-
For 'f','d 'and' e 'conversion codes, the packaged representation will use IEEE 754 binary32, binary64 or binary16 format (corresponding to' f ','d' or 'e' respectively), regardless of the floating-point format used by the platform.
-
'P' format characters are only available for native byte order (select as default or use '@' byte order characters). The byte order character '=' selects to use the small end or large end sorting based on the host system. The struct module will not interpret it as a native sort, so the 'P' format will not be available.
-
IEEE 754 binary16 "half precision" type is in IEEE 754 standard Introduced in the 2008 revision of. It contains one sign bit, five exponential bits and 11 precision bits (10 bits are explicitly stored), which can completely and accurately represent numbers in the approximate range of 6.1e-05 and 6.5e+04. This type is not widely supported by the C compiler: on a typical machine, unsigned short can be used for storage, but it will not be used for mathematical operations. See Wikipedia page half-precision floating-point format Learn more.