python中字符怎么轉(zhuǎn)數(shù)字呢?不知道的小伙伴來看看小編今天的分享吧!
python中字符串str轉(zhuǎn)數(shù)字:
float(str)
int(str)
拓展資料:
1、數(shù)字num轉(zhuǎn)字符串:
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、數(shù)字num轉(zhuǎn)bytes
需將num轉(zhuǎn)為str,再利用codec的encode函數(shù),將str轉(zhuǎn)為bytes:encode(str(num))
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、字符串轉(zhuǎn)bytes
from codec import encode,decode
encode(str)
方法2
bytes(str,'UTF-8')
bytes轉(zhuǎn)字符串
from codec import encode,decode
decode(bytes)12
str(bytes,'UTF-8')1
cmd ='\x02\x73\x54\x49\x20\x03\x0a'
以上就是小編今天的分享了,希望可以幫助到大家。
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com