Python 提供了豐富的數(shù)據(jù)類型來處理各種類型的數(shù)據(jù)。通常,可能會(huì)發(fā)現(xiàn)自己需要將數(shù)據(jù)從一種類型轉(zhuǎn)換為另一種類型,以執(zhí)行操作或確保代碼的兼容性。這個(gè)過程稱為類型轉(zhuǎn)換,Python 提供了多種函數(shù),實(shí)現(xiàn)數(shù)據(jù)類型的轉(zhuǎn)換。在本文中,我們將通過示例來探討這些轉(zhuǎn)換函數(shù)。
int()
int() 函數(shù)將對(duì)象轉(zhuǎn)換為整數(shù)。它可以處理整數(shù)字符串和浮點(diǎn)數(shù),將小數(shù)部分向下舍入。不能處理浮點(diǎn)數(shù)字符串。
a = "10"
b = 3.14
c = 3.9
a_int = int(a)
b_int = int(b)
c_int = int(c)
print(a_int)
print(b_int)
print(c_int)
輸出:
10
3
3
float()
float() 函數(shù)將對(duì)象轉(zhuǎn)換為浮點(diǎn)數(shù)。它可以轉(zhuǎn)換整數(shù)、包含數(shù)字字符的字符串,甚至是科學(xué)記數(shù)法。
a = "10"
b = 3
c = "10.9"
d = 3.14E+3
a_float = float(a)
b_float = float(b)
c_float = float(c)
d_float = float(d)
print(a_float)
print(b_float)
print(c_float)
print(d_float)
輸出:
10.0
3.0
10.9
3140.0
str()
str() 函數(shù)將對(duì)象轉(zhuǎn)換為字符串。常用于將非字符串與字符串連接起來。
a = 10
b = 3.14
a_str = str(a)
b_str = str(b)
c_str = a_str + b_str
d = a + b
print(a_str)
print(b_str)
print(c_str)
print(d)
輸出:
10
3.14
103.14
13.14
list()
list() 函數(shù)將可迭代對(duì)象(如元組或字符串)轉(zhuǎn)換為列表。
a = "abc"
b = (1, 2, 3)
c = ("1", "2", "3")
a_list = list(a)
b_list = list(b)
c_list = list(c)
print(a_list)
print(b_list)
print(c_list)
運(yùn)行結(jié)果:
['a', 'b', 'c']
[1, 2, 3]
['1', '2', '3']
tuple()
tuple() 函數(shù)將可迭代對(duì)象轉(zhuǎn)換為元組。
a = "abc"
b = [1, 2, 3]
c = ["1", "2", "3"]
a_tuple = tuple(a)
b_tuple = tuple(b)
c_tuple = tuple(c)
print(a_tuple)
print(b_tuple)
print(c_tuple)
運(yùn)行結(jié)果:
('a', 'b', 'c')
(1, 2, 3)
('1', '2', '3')
set()
set() 函數(shù)將可迭代對(duì)象轉(zhuǎn)換為集合,并刪除重復(fù)的元素。
a = "abc"
b = [1, 2, 3, 3]
c = ["1", "2", "3"]
a_set = set(a)
b_set = set(b)
c_set = set(c)
print(a_set)
print(b_set)
print(c_set)
輸出:
{'a', 'c', 'b'}
{1, 2, 3}
{'1', '3', '2'}
dict()
dict() 函數(shù)可以將嵌套的可迭代對(duì)象(如元組)轉(zhuǎn)換為字典。
a = [("a", 1), ("b", 2), ("c", 3)]
a_dict = dict(a)
print(a_dict)
輸出:
{'a': 1, 'b': 2, 'c': 3}
bool()
bool() 函數(shù)將對(duì)象轉(zhuǎn)換為布爾值。它將數(shù)值為零值和空值返回 “False” ,將非零數(shù)字和非空容器返回 “True”。
a = ""
b = 123
c = "abc"
a_bool = bool(a)
b_bool = bool(b)
c_bool = bool(c)
print(a_bool)
print(b_bool)
print(c_bool)
輸出:
False
True
True
complex()
complex() 函數(shù)將整數(shù)、浮點(diǎn)數(shù)或數(shù)值型字符串轉(zhuǎn)換為復(fù)數(shù)。
a = 3.14
b = 123
c = "123"
a_complex = complex(a)
b_complex = complex(b)
c_complex = complex(c)
print(a_complex)
print(b_complex)
print(c_complex)
輸出:
(3.14+0j)
(123+0j)
(123+0j)
bytes()
bytes() 函數(shù)將字符串轉(zhuǎn)換為字節(jié)對(duì)象。
a = "abc"
b = "3.14"
a_bytes = bytes(a, encoding="utf-8")
b_bytes = bytes(b, encoding="utf-8")
print(a_bytes)
print(b_bytes)
輸出:
b'abc'
b'3.14'
bytearray()
bytearray() 函數(shù)從字符串或可迭代對(duì)象創(chuàng)建可變字節(jié)數(shù)組。
a = "abc"
b = "3.14"
a_bytearray = bytearray(a, encoding="utf-8")
b_bytearray = bytearray(b, encoding="utf-8")
print(a_bytearray)
print(b_bytearray)
輸出:
bytearray(b'abc')
bytearray(b'3.14')
ord()
ord() 函數(shù)返回字符的 ASCII 數(shù)值。
a = "a"
a_ord = ord(a)
print(a_ord)
輸出:
97
chr()
chr() 函數(shù)將 ASCII 數(shù)值轉(zhuǎn)換為字符。
a = 88
a_chr = chr(a)
print(a_chr)
輸出:
x