python入门:NO. 6 数据转换

说好的每天更新我却鸽了好多天,对8起。
现在再也咩有十四运拦着我更新了,每天更新就来!
我flag立这儿了

前面说到了三种基本的数据类型:str、int、float,现在一些数据它们的类型呢?
很简单,用专用的type函数查看:

who = '我' 
destination = 'python世界' 
number = 4134513 
print(type(who)) 
print(type(destination)) 
print(type(number)) 

>> 
<class 'str'> 
<class 'str'> 
<class 'int'>

查看了上面三个数据的类型,现在想要把它们在一句话中打印出来,它们类型不同,怎么做?
在python里这个很简单——强制类型转换
str():将其他类型数据转化成字符串

who = '我的' 
destination = 'python世界' 
number = 4134513 
code = '密码' 
print ( who + destination + code + str(number)) 

>>我的python世界密码4134513

非常简单,顺便说一句,也可以用引号将数据转化成字符串。
int():将其他数据类型转化为整数

print (int(3.8)) 

>>3

float类型转int型将直接抹零。
float():将其他数据类型转化为浮点数

print (float(98)) 

>>98.0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值