数据类型转换-str()-int()-float函数
print('# 为什么要数据类型转换?——将不同数据类型的数据拼接在一起')
name='张三'
age=20
print(type(name),type(age))
print('我叫'+name+','+'今年'+str(age)+'岁')
print('''
# int()
# 只截取整数部分
# 可以转布尔类型
# 不能转小数串
# 不能转字母
''')
a=10
b=198.8
c=False
print(type(a),type(b),type(c))
print(type(a),type(b),type(c),type(str(a)),type(str(b)),type(str(c)))
s1='128'
print(int(s1))
f1=98.7
print(int(f1))
ff=True
print(int(ff))
s2='76.77'
# print(int(s2))
s3='hello'
# print(int(s3))
print('# float() 其它类型转为float型')
s1='128.98'
print(float(s1))
s2=76
print(float(s2))
s3='hello'
# print(float(s3))
ff=True
print(float(ff))
i=98
print(float(i))
运行结果:
# 为什么要数据类型转换?——将不同数据类型的数据拼接在一起
<class 'str'> <class 'int'>
我叫张三,今年20岁
# int()
# 只截取整数部分
# 可以转布尔类型
# 不能转小数串
# 不能转字母
<class 'int'> <class 'float'> <class 'bool'>
<class 'int'> <class 'float'> <class 'bool'> <class 'str'> <class 'str'> <class 'str'>
128
98
1
# float() 其它类型转为float型
128.98
76.0
1.0
98.0
Process finished with exit code 0