【python实战之】数据类型转换-str()-int()-float()函数

数据类型转换-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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值