python 數字與字串轉換
例子程式:
數字轉字串
#!/usr/bin/python
#-*- coding:utf-8 -*-
if __name__ == "__main__":
a = 64
print 'type(a)=', type(a)
print 'a=', a
print
b = oct(a)
print 'type(b),', type(b)
print 'b=', b
c = hex(a)
print 'type(c),', type(c)
print 'c=', c
d = bin(a)
print 'type(d),', type(d)
print 'd=', d
結果:
c:\python27\python.exe e:/python/work/thread_t1/number_test.py
type(a)= a= 64
type(b), b= 0100
type(c), c= 0x40
type(d), d= 0b1000000
process finished with exit code 0
字串轉數字#!/usr/bin/python
#-*- coding:utf-8 -*-
if __name__ == "__main__":
a = '64'
print 'type(a)=', type(a)
print 'a=', a
print
b = int(a)
print 'type(b),', type(b)
print 'b=', b
print
c = '40'
print 'type(c),', type(c)
print 'c=', c
d = int(c, 16)
print 'type(d),', type(d)
print 'd=', d
print
e = '100'
print 'type(e),', type(e)
print 'e=', e
f = int(e, 8)
print 'type(f),', type(f)
print 'f=', f
print
g = '1000000'
print 'type(g),', type(g)
print 'g=', g
h = int(g, 2)
print 'type(h),', type(h)
print 'h=', h
log 為:
•ord是unicode ordinal的縮寫,即編號
•chr是character的縮寫,即字元
•ord和chr是互相對應轉換的.
•但是由於chr侷限於ascii,長度只有256,於是又多了個unichr.
Python字串與數字轉換
例項 a 18 b string.atoi a,16 print b 24 a 0x18 b string.atoi a,16 print b 24 a 18 c string atoi a print c 18數字轉換成字串 接上面 d i c 10進製表示 print d 18 type d s...
數字與字串轉換
題目大意 給定兩個數 i 和 j 將數字 i j 翻轉後按公升序排列輸出。include include include include includeusing namespace std struct node num 55 翻轉字串 char strrev char s int len str...
python字串與數字之間的轉換
usr bin python death age 80name input your name age input your age input 接受的所有資料都是字串,即便你輸入的是數字,但依然會被當成字串來處理 print type age int inteager 整數 把字串轉成int,用i...