Python入门 - 类型转换
int, float
print(int(1.2)) # 1 print(int("1")) # 1 print(int("0b11", 2)) # 3 print(int("27", 8)) # 23 print(int("0xF", 16)) # 15 print(int("0x1F", 16)) # 31 print(float("1.2")) # 1.2
进制转换
print(oct(10)) # 0o12 print(hex(12)) # 0xc print(bin(3)) # 0b11
bool
print(bool(1)) # True print(bool(0)) # False print(bool(2)) # True print(bool(-1)) # True print(bool(-2)) # True
字符串
print(str(1)) # "1" print(str(1.2)) # "1.2" print(str(True)) # "True" print(str([1, 2, 3])) # "[1, 2, 3]" tuple_1 = (1, "a") print(str(tuple_1)) # "(1, 'a')"