1、int():将字符串转换成整数类型
>>> int("456");
456
>>>
2、float():将字符串转换成浮点类型
>>> float("89.236");
89.236
>>>
3、complex():创建一个复数,参数为实部和虚部
>>> complex("58","96");
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
complex("58","96");
TypeError: complex() can't take second arg if first is a string
>>> complex(58,89);
(58+89j)
>>>
4、str():将其它数据类型转换成字符串
>>> str(True);
'True'
>>> str(678);
'678'
>>> str([1,2,3,4,5,6,7,8,9,,9]);
SyntaxError: invalid syntax
>>> str([1,2,3,4,5,6,7,8,9]);
'[1, 2, 3, 4, 5, 6, 7, 8, 9]'
>>>
5、repr():将括号中的参数转换为表达式字符串
>>> repr("x")
"'x'"
>>> repr("56");
"'56'"
>>>
6、eval():计算在字符串中的有效表达式,并返回一个对象
>>> eval("34+78");
112
>>> eval("45+'sr'");
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
eval("45+'sr'");
File "<string>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>>
7、chr():将整数转换成一个字符
>>> chr(80);
'P'
>>> chr(123);
'{'
>>> chr(562);
'Ȳ'
>>> chr(345);
'ř'
>>>
8、ord():将一个字符转换成对应的整数值
>>> ord("y");
121
>>> ord("h");
104
>>> ord("d");
100
>>> ord("?");
63
>>>
9、oct():将一个整数转换成一个八进制的字符串
>>> oct(52);
'0o64'
>>> oct(7895552323232323);
'0o340317320742021103'
>>> oct(-67888);
'-0o204460'
>>> oct(5555555);
'0o25142543'
>>>
10、hex():将一个整数转换成一个十六进制字符串
>>> hex(5665);
'0x1621'
>>> hex(-76877);
'-0x12c4d'
>>> hex(89k);
SyntaxError: invalid syntax
>>>