python String模塊在實(shí)際應(yīng)用的代碼介紹
python String模塊是一種應(yīng)用相當(dāng)廣泛,在實(shí)際操作中功能十分強(qiáng)大的計(jì)算機(jī)語(yǔ)言,但是懂得如何簡(jiǎn)捷的運(yùn)用python string模塊的這一語(yǔ)言的人卻不占大多數(shù),以下的內(nèi)容就是對(duì)python String模塊在實(shí)際運(yùn)用的解析。
推薦使用str類,而不是string模塊:String模塊提供了常用的字符串處理函數(shù);這些函數(shù)通??梢栽趕tr類中找到對(duì)應(yīng);python String模塊內(nèi)提供的某些常量還是非常有用的。
string成員常量:
- ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHI
JKLMNOPQRSTUVWXYZ'- ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
- ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij
klmnopqrstuvwxyz'- lowercase = 'abcdefghijklmnopqrstuvwxyz'
- uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- digits = '0123456789'
- hexdigits = '0123456789abcdefABCDEF'
- octdigits = '01234567'
- whitespace = '\t\n\x0b\x0c\r '
- punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
- printable = '0123456789abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTU...
成員函數(shù):
- atof(s) -> float
- >>> atof = string.atof
- >>> atof("3.14")
- 3.1400000000000001
- >>> atof("1")
可以處理整數(shù)
- 1.0
- >>> atof("-9")
可以處理正負(fù)號(hào)不可以在數(shù)字字符尾端加‘f’
- Traceback (most recent call last):
- File "<pyshell#24>", line 1, in <module>
- atof("3.14f")
- File "C:\Python26\lib\string.py", line 386, in atof
- return _float(s)
- ValueError: invalid literal for float(): 3.14f
- >>> atof("s3.14")
遇到錯(cuò)誤輸入,拋出異常
- Traceback (most recent call last):
- File "<pyshell#26>", line 1, in <module>
- atof("s3.14")
- File "C:\Python26\lib\string.py", line 386, in atof
- return _float(s)
- ValueError: invalid literal for float(): s3.14
- atoi(s [,base]) -> int
轉(zhuǎn)換字符串S為整數(shù),base默認(rèn)為10。以上的文章就是對(duì)python String模塊的相關(guān)介紹。
【編輯推薦】
- python語(yǔ)法入門(mén)中導(dǎo)入import與from時(shí)的代碼示例
- Python 中文亂碼問(wèn)題的分析的具體方案介紹
- python 編程語(yǔ)言在操作文件編碼格式的應(yīng)用
- Python安裝配置的具體步驟的相關(guān)介紹
- Python 配置文件的實(shí)際應(yīng)用解析