python中变量类型转换

 

1、

>>> a = 123
>>> a
123
>>> type(a)
<class 'int'>
>>> b = float(a)   ## 整数转化为浮点
>>> b
123.0
>>> type(b)
<class 'float'>
>>> c = str(a)     ## 整数转化为字符串
>>> c
'123'
>>> type(c)
<class 'str'>

 

2、

>>> a = 123.000
>>> type(a)
<class 'float'>
>>> b = int(a)   ## 浮点转化为整数
>>> b
123
>>> type(b)
<class 'int'>
>>> c = str(a)   ## 浮点转化为字符串
>>> c
'123.0'
>>> type(c)
<class 'str'>

 

3、

>>> a = "123"
>>> type(a)
<class 'str'>
>>> b = int(a)   ## 字符串转化为整数
>>> b
123
>>> type(b)
<class 'int'>
>>> c = float(a)   ## 字符串转化为浮点数
>>> c
123.0
>>> type(c)

 

posted @ 2021-02-25 15:13  小鲨鱼2018  阅读(188)  评论(0编辑  收藏  举报