预留广告位
预留广告位
预留广告位
当前位置:首页  »  科技  »  科技百科  »  文章  »  python 字符转数字

python 字符转数字

来源:网络 转载:好巧合 时间:2022-01-22 21:31
导读python 字符转数字,python中字符串str转数字:float(str);int(str)。数字num转字符串:str(num)。数字num转bytes:需将num转为str,再利用codec的encode函数,将str转为bytes:encode(str(num))。

python中字符怎么转数字呢?不知道的小伙伴来看看小编今天的分享吧!

python中字符串str转数字:

float(str)

int(str)

拓展资料:

1、数字num转字符串:

str(num)

代码如下:

a ='1234'

print('a = ',a)

print(type(a))

b = int(a)

print('b = ',b)

print(type(b))

c = float(a)

print('c = ',c)

print(type(c))

d = str(b)

print('d = ',d)

print(type(d))

f = 1.234

ff = str(f)

print('ff = ',ff)

print(type(ff))

2、数字num转bytes

需将num转为str,再利用codec的encode函数,将str转为bytes:encode(str(num))

bytes转数字

int(bytes)

float(bytes)

代码如下:

from codecs import encode, decode

b = b'1.234'

print('b = ',b)

print(type(b))

c = float(b)

print('c = ',c)

print(type(c))

d = str(c)

e = encode(d)

print('e = ',e)

print(type(e))

f = decode(e)

print('f = ',f)

print(type(f))

3、字符串转bytes

方法1

from codec import encode,decode

encode(str)

方法2

bytes(str,'UTF-8')

bytes转字符串

方法1

from codec import encode,decode

decode(bytes)12

方法2

str(bytes,'UTF-8')1

数字表达字符串

cmd ='\x02\x73\x54\x49\x20\x03\x0a'

以上就是小编今天的分享了,希望可以帮助到大家。

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。

预留广告位
标签:python  字符转数字  
Top