Python的字符串
ord()//函數(shù)獲取字符的整數(shù)表示
chr()//函數(shù)把編碼轉(zhuǎn)換為對應(yīng)的字符
encode()//方法可以編碼為指定的bytes
decode()//吧bytes變?yōu)閟tr钞翔,如果bytes中只有一小部分無效的字節(jié)丹拯,可以傳入errors='ignore'忽略錯誤的字節(jié)
len()//len()函數(shù)計算的是str的字符數(shù),如果換成bytes,len()函數(shù)就是計算字節(jié)數(shù)
格式化
占位符 |
替換內(nèi)容 |
%d |
整數(shù) |
%f |
浮點數(shù) |
%s |
字符串 |
%x |
十六進制整數(shù) |
format()
代碼案例
# -*- coding:utf-8 -*-
print(ord('A'))
print(ord('中'))
print(chr(66))
print(chr(25991))
print('ABC'.encode('ascii'))
print('中文'.encode('utf-8'))
print(b'\xe4\xb8\xad\xff'.decode('utf-8',errors='ignore'))
print(len('ABC'))
print(len('中文'.encode('utf-8')))
print('Hello , %s ' % 'world')
print('Hi %s , you have $%d' % ('Michael',10000))
print('%2d-%02d' % (3,1))
print('%.2f' % 3.14159)
print('growth rate: %d %%' % 7)
print('Hello, {0},{1:.1f}%'.format('小明',17.125))