python强制类型转换
string,int,float,boolean之间的强制转换
·
1.强转为int
string->int
a = 'string'
print(int(a))
a = '1.2'
print(int(a))
a = '12'
print(int(a))
string仅在无特殊字符(包括小数点)且全为数字的情况下可强转为float
boolen->int
a = True
b = False
print(int(a))
print(int(b))
float->int
a = 1.1
print(int(a))
2.强转为string
int->string
a = 1
print(str(a))
float->string
a = 1.0
print(str(a))
boolean->string
a = True
print(str(a))
3.强转为boolean
string->boolean
a = 'string'
print(bool(a))
int->boolean
a = 1
print(bool(a))
float->boolean
a = 1.1
print(bool(a))
4.强转为float
int->float
a = 1
print(float(a))
string->float
a = 'string'
print(float(a))
b = 'ab12'
print(float(b))
c = '1-1'
print(float(c))
d = '12.0'
print(float(d))
string仅在无特殊字符(包括小数点)且全为数字的情况下可强转为float
boolean->float
a = True
print(float(a))
更多推荐
已为社区贡献2条内容
所有评论(0)