数据类型转换
*为什么需要数据类型转换
将不同数据类型的数据拼接在一起
name='张三'
age=25
print(type(name),type(age)) #说明name与age的数据类型不相同
#print('我叫'+name,'今年'+age+'岁') #当将str类型与int类型进行连接时报错 解决方案:类型转换
print('我叫'+name,'今年'+str(age)+'岁') #将int类型通过str()函数转成了str类型
print('-------------str()将其他类型转成str类型---------------')
a=10
b=198.8
c=False
print(type(a),type(b),type(c))
print(str(a),str(b),str(c),type(str(a)),type(str(b)),type(str(c)))
print('-------------int()将其他类型转成int类型---------------')
s1='128'
f1=98.7
s2='76.77'
ff=True
s3='Hello'
print(type(s1),type(f1),type(s2),type(ff),type(s3))
print(int(s1),type(int(s1))) #将str转成int类型,字符串为数字串
print(int(f1),type(int(f1))) #float转成int类型,截取整数部分,舍掉小数部分
#print(int(s2),type(int(s2))) #将str转成int类型报错,因为字符串为小数串
print(int(ff),type(int(ff))) #将布尔类型转换为int类型
#print(int(s3),type(int(s3))) #将str转成int类型时,字符串必须为数字串(整数),非数字串不允许转换
显示结果为:
<class 'str'> <class 'int'>
我叫张三 今年25岁
-------------str()将其他类型转成str类型---------------
<class 'int'> <class 'float'> <class 'bool'>
10 198.8 False <class 'str'> <class 'str'> <class 'str'>
-------------int()将其他类型转成int类型---------------
<class 'str'> <class 'float'> <class 'str'> <class 'bool'> <class 'str'>
128 <class 'int'>
98 <class 'int'>
1 <class 'int'>
print('------------------float()函数,将其他数据类型转成float类型')
s1='128'
f1=98.7
s2='76.77'
ff=True
s3='Hello'
i=98
print(type(s1),type(f1),type(s2),type(ff),type(s3),type(i))
print(float(s1),type(float(s1)))
print(float(s2),type(float(s2)))
print(float(ff),type(float(ff)))
# print(float(s3),type(float(s3))) #字符串中的数据如果是非数字串,则不允许转换
print(float(i),type(float(i)))
显示结果为:
------------------float()函数,将其他数据类型转成float类型
<class 'str'> <class 'float'> <class 'str'> <class 'bool'> <class 'str'> <class 'int'>
128.0 <class 'float'>
76.77 <class 'float'>
1.0 <class 'float'>
98.0 <class 'float'>