a = 3
b = 5
a = 3.0
b = 5.2
a = “3.0”
b = “3.ef”
type()
1、将整数转换成浮点数
a = 3 b = float(a) print(b) print(type(b))
2、将浮点数转换成整数
a = 3.2 b = int(a) print(b) print(type(b))
3、将字符串转换成整数
a = "125" b = int(a) print(b) print(type(b))
4、将整数转换成字符串
a = 358 b = str(a) print(b) print(type(b))
5、将字符串转换成浮点数
a = "3.879" b = float(a) print(b) print(type(b))
6、将浮点数转换成字符串
a = 3.7532 b = str(a) print(b) print(type(b))
如果要转换成的int型或是float型不是一个数,就会出错
a = "39a" b = int(a)
a = "39a" b = float(a)
原文:https://www.cnblogs.com/luhouxiang/p/11296207.html