Python常见基础学习---类型转换

类型转换:

int()     # str()将其他转为字符类型

str()     # int()将其他转为整型

float()  # float()将其他转为浮点数

# 类型转换

name='张三'
age=21

print(type(name),type(age))
print('我叫'+name+'今年'+str(age)+'岁了')

# str()将其他转为字符类型
s1=123
s2=12.1
s3=True

print(str(s1),str(s2),str(s3),type(str(s1)),type(str(s2)),type(str(s3)))

# int()将其他转为整型
i1=12.1
i2=False
i3='234'
i4='12.1' # 不可
i5='纽带一批'# 不可
print(int(i1),int(i2),int(i3))

# float()将其他转为浮点数
i1=12
i2=False
i3='234'
i4='12.1' 
i5='纽带一批'# 不可
print(float(i1),float(i2),float(i3),float(i4))





#实验结果:
<class 'str'> <class 'int'>
我叫张三今年21岁了
123 12.1 True <class 'str'> <class 'str'> <class 'str'>
12 0 234
12.0 0.0 234.0 12.1

注意事项:

【1】:print输出括号里面只能是同类型的

【2】:int()转换,不可转换字符串中非整数

【3】:float ()转换,不可转换字符串中字符串

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静海彭于晏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值