python string模块template_Python标准库笔记(1) — string模块
String模塊包含大量實(shí)用常量和類,以及一些過時的遺留功能,并還可用作字符串操作。
1. 常用方法
常用方法描述str.capitalize()把字符串的首字母大寫str.center(width)將原字符串用空格填充成一個長度為width的字符串,原字符串內(nèi)容居中str.count(s)返回字符串s在str中出現(xiàn)的次數(shù)str.decode(encoding=’UTF-8’,errors=’strict’)以指定編碼格式解碼字符串str.encode(encoding=’UTF-8’,errors=’strict’)以指定編碼格式編碼字符串str.endswith(s)判斷字符串str是否以字符串s結(jié)尾str.find(s)返回字符串s在字符串str中的位置索引,沒有則返回-1str.index(s)和find()方法一樣,但是如果s不存在于str中則會拋出異常str.isalnum()如果str至少有一個字符并且都是字母或數(shù)字則返回True,否則返回Falsestr.isalpha()如果str至少有一個字符并且都是字母則返回True,否則返回Falsestr.isdigit()如果str只包含數(shù)字則返回 True 否則返回 Falsestr.islower()如果str存在區(qū)分大小寫的字符,并且都是小寫則返回True 否則返回Falsestr.isspace()如果str中只包含空格,則返回 True,否則返回 Falsestr.istitle()如果str是標(biāo)題化的(單詞首字母大寫)則返回True,否則返回Falsestr.isupper()如果str存在區(qū)分大小寫的字符,并且都是大寫則返回True 否則返回Falsestr.ljust(width)返回一個原字符串左對齊的并使用空格填充至長度width的新字符串str.lower()轉(zhuǎn)換str中所有大寫字符為小寫str.lstrip()去掉str左邊的不可見字符str.partition(s)用s將str切分成三個值str.replace(a, b)將字符串str中的a替換成bstr.rfind(s)類似于 find()函數(shù),不過是從右邊開始查找str.rindex(s)類似于 index(),不過是從右邊開始str.rjust(width)返回一個原字符串右對齊的并使用空格填充至長度width的新字符串str.rpartition(s)類似于 partition()函數(shù),不過是從右邊開始查找str.rstrip()去掉str右邊的不可見字符str.split(s)以s為分隔符切片strstr.splitlines()按照行分隔,返回一個包含各行作為元素的列表str.startswith(s)檢查字符串str是否是以s開頭,是則返回True,否則返回Falsestr.strip()等于同時執(zhí)行rstrip()和lstrip()str.title()返回”標(biāo)題化”的str,所有單詞都是以大寫開始,其余字母均為小寫str.upper()返回str所有字符為大寫的字符串str.zfill(width)返回長度為 width 的字符串,原字符串str右對齊,前面填充0
2.字符串常量
常數(shù)含義string.ascii_lowercase小寫字母’abcdefghijklmnopqrstuvwxyz’string.ascii_uppercase大寫的字母’ABCDEFGHIJKLMNOPQRSTUVWXYZ’string.ascii_lettersascii_lowercase和ascii_uppercase常量的連接串string.digits數(shù)字0到9的字符串:’0123456789’string.hexdigits字符串’0123456789abcdefABCDEF’string.letters字符串’abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’string.lowercase小寫字母的字符串’abcdefghijklmnopqrstuvwxyz’string.octdigits字符串’01234567’string.punctuation所有標(biāo)點(diǎn)字符string.printable可打印的字符的字符串。包含數(shù)字、字母、標(biāo)點(diǎn)符號和空格string.uppercase大學(xué)字母的字符串’ABCDEFGHIJKLMNOPQRSTUVWXYZ’string.whitespace空白字符 ‘\t\n\x0b\x0c\r ‘
3.字符串模板Template
通過string.Template可以為Python定制字符串的替換標(biāo)準(zhǔn),下面是具體列子:
>>>from string import Template
>>>s = Template('$who like $what')
>>>print s.substitute(who='i', what='python')
i like python
>>>print s.safe_substitute(who='i') # 缺少key時不會拋錯
i like $what
>>>Template('${who}LikePython').substitute(who='I') # 在字符串內(nèi)時使用{}
'ILikePython'
Template還有更加高級的用法,可以通過繼承string.Template, 重寫變量delimiter(定界符)和idpattern(替換格式), 定制不同形式的模板。
import string
template_text = ''' Delimiter : $de Replaced : %with_underscore Ingored : %notunderscored '''
d = {'de': 'not replaced',
'with_underscore': 'replaced',
'notunderscored': 'not replaced'}
class MyTemplate(string.Template):
# 重寫模板 定界符(delimiter)為"%", 替換模式(idpattern)必須包含下劃線(_)
delimiter = '%'
idpattern = '[a-z]+_[a-z]+'
print string.Template(template_text).safe_substitute(d)? # 采用原來的Template渲染
print MyTemplate(template_text).safe_substitute(d)? # 使用重寫后的MyTemplate渲染
輸出:
Delimiter : not replaced
Replaced : %with_underscore
Ingored : %notunderscored
Delimiter : $de
Replaced : replaced
Ingored : %notunderscored
原生的Template只會渲染界定符為$的情況,重寫后的MyTemplate會渲染界定符為%且替換格式帶有下劃線的情況。
4.常用字符串技巧
1.反轉(zhuǎn)字符串
>>> s = '1234567890'
>>> print s[::-1]
0987654321
2.關(guān)于字符串鏈接
盡量使用join()鏈接字符串,因為’+’號連接n個字符串需要申請n-1次內(nèi)存,使用join()需要申請1次內(nèi)存。
3.固定長度分割字符串
>>> import re
>>> s = '1234567890'
>>> re.findall(r'.{1,3}', s)? # 已三個長度分割字符串
['123', '456', '789', '0']
4.使用()括號生成字符串
sql = ('SELECT count() FROM table '
'WHERE id = "10" '
'GROUP BY sex')
print sql
SELECT count() FROM table WHERE id = "10" GROUP BY sex
5.將print的字符串寫到文件
>>> print >> open("somefile.txt", "w+"), "Hello World"? # Hello World將寫入文件somefile.txt
轉(zhuǎn)載于:https://my.oschina.net/jhao104/blog/829775
總結(jié)
以上是生活随笔為你收集整理的python string模块template_Python标准库笔记(1) — string模块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone 有锁机黑解相关问题汇总
- 下一篇: julia与python对比_有人说Ju