Python中的string模块详解——string的中文API文档 (i641015077) #string 模块详解(翻译help文档)(Python 2.7.6)#函数部分atof(s) #返回s表示的float类型数string.atof(1)atoi(s, base=10)#返回s表示的整形数,base默认值是10,表示转换后的整型数的进制 #base取0时,根据字符串s的首字符确定进制string.atoi("123", 8)string.atoi("0123", 0)#这2种表示形式代表同一个八进制数83atol(s, base=10)#返回s表示的长整型数,具体大小忘了,同atoi(s, base)相似,返回值后面带L标识string.atol("123", 8)#返回值是:83Lcapitalize(s)#返回s的拷贝,并且将s首字符转换为大写形式,如果首字符不是字母,则保持原样输出string.capitalize("asdf")#输出:Asdfstring.capitalize("+asdf")#输出:+asdfcapwords(s, seq=None)#返回s的拷贝,并且用seq将s分割,每一部分的首字符大写string.capwords("asdfasdf")#没指定分割字符,则首字符大写,同capitalize(s) #输出为:Asdfasdfstring.capwords("+asdf+asd+f", "d")#每个部分首字符为非字母时,原样输出 #输出为:+asdF+asd+f center(s, width, *args)#返回指定长度域,且s居中的字符串,其余部分以字符*args填充,注:*args只能为单字符string.center("asdf", 20) #未指定填充字符则以空格填充,输出为: ' asdf ' string.center("asdf", 20, "+")#输出为:'++++++++asdf++++++++'count(s, *args)#返回字符串*args在s中出现的次数string.count("asdfasdfas", "asdf"[0:2])#输出"as"在s中的出现次数,输出为:3expandtabs(s, tabsize=8)#返回s的拷贝,并且把s中的tab字符替换为指定长度,默认长度为8个字符长度string.expandtabs("asdf asdf")#输出为:'asdf asdf'string.expandtabs("asdf asdf", 0)#输出为:'asdfasdf'find(s, *args)#返回字符串*args在s中第一次出现的索引,未找到指定字符串则输出-1string("asdffsda", "asdf"[1:3])#输出为1rfind(s, *args)#返回字符串*args在s中最后一次出现的索引,未找到指定字符串则输出-1string.rfind("asdfasdfas", "as")#输出为:8string.rfind("asdfasdfas", "sa")#输出为:-1index(s, *args)#和find(s, *args)相似,区别在于没找到指定字符串则报ValueError:substring not foundrindex(s, *args)#和rfind(s, *args)相似,区别在于没找到指定字符串则报ValueError:substring not foundjoin(words, seq=' ')#返回序列类型中每个子串组成的字符串,以seq字符分隔,默认分隔符为' ',分隔符可以为字符串string.join(["sadf", "+", "xyz"])#输出为:'sadf + xyz'string.join("sadf")#输出为:'s a d f'string.join("sadf", '')#输出为:'sadf'string.join("asdf", "xx")#输出为:'axxsxxdxxf'joinfields(list, seq=' ')#与join(words, seq=' ')作用相同string.joinfields("asdf", "xy")#输出为:'axysxydxyf'ljust(s, width, *args)#返回一个字符串,字符串长度为width,s在此字符串中左对齐,*args为填充字符(默认为空格)string.ljust("asdf", 20)#输出为:'asdf ' string.ljust("asdf", 20, "0")#输出为:'asdf0000000000000000'lower(s)#返回字符串s的拷贝,并且将s中的所有大写字母替换成小写,非字母字符不变string.lower("aSdF123+")#输出为:'asdf123+'upper(s)#返回字符串s的拷贝,并且将s中的所有小写字母替换成大写,非字母字符不变string.upper("aSdF123+")#输出为:'ASDF123+'swapcase(s)#输出字符串s的拷贝,并且将s中的所有大写字母替换成小写,小写字母替换成大写 #非字母元素不变string.swapcase("aSdF123+")#输出为:AsDf123+'rjust(s, width, *args)#返回一个字符串,字符串长度为width,s在此字符串中右对齐,*args为填充字符(默认为空格)string.rjust("asdf", 20)#输出为:' asdf'string.rjust("asdf", 20, "x")#输出为:'xxxxxxxxxxxxxxxxasdf'strip(s, chars=None)#返回字符串s的拷贝,并且将其左右两侧的chars字符串中包含的字符移除string.strip("asdffdsa", 'as')#输出为:'dffd'string.strip(" asdffdsa ")#输出为:'asdffdsa',在未指定chars的情况下,移除左右两侧的空格(tab字符等也回被移除。即wihtespace)lstrip(s, chars=None)#返回字符串s的拷贝,并且将其左侧的chars字符串中包含的字符移除string.lstrip(" asdf")#输出为:'sadf',默认移除左边的空格string.lstrip("assassaadfdfdfd", "sa")#输出为:'dfdfdfd',移除字符串s中左边的所有字符'a'和字符's'rstrip(s, chars=None)#返回字符串s的拷贝,并且将其右侧的chars字符串中包含的字符移除zfill(x, width)#返回一个指定长度width的字符串,在字符串左边用数字0填充string.zfill("123", 10)#输出为:'0000000123'string.zfill("asd", 10)#输出为:'0000000asd'split(s, seq=None, maxsplit=-1)#返回一个列表,列表里的元素是s的子串,用seq分割,string.split("asf sad f") #输出为:['asf', 'sad', 'f']。未指定分隔符时,默认用whitespace分割string.split("asd fewa afe", "a")#输出为:['', 'sd few', '\t', 'fe']string.split("asd fewa afe", "as")#输出为:['', 'd fewa\tfe']。以字符串为分隔符string.split("a123a123a21a", "a", 2)#输出为:['', '123', '123a21a'],maxsplit代表分割次数splitfields(s, seq=None, maxsplit=-1)#与split用法、功能相同rsplit(s, seq=None, maxsplit=-1)#返回一个列表,列表里的元素是s的子串,用seq分割,string.rsplit("asf sad f") #输出为:['asf', 'sad', 'f']。未指定分隔符时,默认用whitespace分割string.rsplit("asd fewa afe", "a")#输出为:['', 'sd few', '\t', 'fe']string.rsplit("asd fewa afe", "as")#输出为:['', 'd fewa\tfe']。以字符串为分隔符string.rsplit("a123a123a21a", "a", 2)#输出为:['a123aa123', '21', ''],maxsplit代表分割次数 #与split区别在于rsplit从右边开始分割maketrans(frm, to)#返回一个用于translate()的table,长度为256bytes,其中frm和to长度相同translate(s, table, deletions='')#返回字符串s的拷贝,并且将其中的字符按table的对应关系一一替换,并且移除deletions字符string.translate("abcdef", string.maketrans("ac", "31"))#输出为:'3b1def'string.translate(" abcdef ", string.maketrans("ac", "31"))#输出为:'\t3b1def ')string.translate("abcdefe", string.maketrans("ac", "31"), "d")#输出为:'3b1efe'string.translate("abcdefe", string.maketrans("ac", "31", "da"))#输出为:'b1efe'。先移除s中所有的deletions字符,再转换对应字符replace(s, old, new, maxreplace=-1)#返回字符串s的拷贝,并且将s中的old字符串替换成new字符串,maxreplace为最大替换次数string.replace("asdf", "as", "123")#输出为:"123df"string.replace("asdfasdfasdf", "as", "123", 2)#输出为:'123df123dfasdf'。最多替换2次#常量部分ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'digits = '0123456789'hexdigits = '0123456789abcdefABCDEF'letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'lowercase = 'abcdefghijklmnopqrstuvwxyz'octdigits = '01234567'printable = ...punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'whitespace = '\t\n\x0b\x0c\r '</pre><pre class="python" name="code">#还缺少两个类:class Formatter(__buildin__.object)和class Template(__buildin__.object),以后有时间补上#算是学习心得,以便日后回顾,有不足之处请多指教!
|
|