網站首頁 學習教育 IT科技 金融知識 旅遊規劃 生活小知識 家鄉美食 養生小知識 健身運動 美容百科 遊戲知識 綜合知識
當前位置:趣知科普吧 > IT科技 > 

字元轉數字|python

欄目: IT科技 / 發佈於: / 人氣:2.99W

python中字元怎麼轉數字呢?不知道的小夥伴來看看小編今天的分享吧!

python中字元串str轉數字:

float(str)

int(str)

python 字元轉數字

拓展資料:

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

python 字元轉數字 第2張

方法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 ='x02x73x54x49x20x03x0a'