标签:als 标题 stdin 默认 分隔符 rac port from display
str.ljust(width[, fillchar]) >>> ‘ hello world‘.ljust(20) ‘ hello world ‘ >>> ‘ hello world‘.ljust(20,‘=‘) ‘ hello world=====‘
>>> ‘HellO WorlD‘.lower() ‘hello world‘ >>> ‘HELLO WORLD‘.lower() ‘hello world‘ >>> ‘HelLO Wo123lD‘.lower() ‘hello wo123ld‘
>>> ‘ hello world‘.lstrip() ‘hello world‘ >>> ‘%%%hello world‘.lstrip(‘%‘) ‘hello world‘ >>> ‘%%%h%ello world‘.lstrip(‘%‘) ‘h%ello world‘
>>> ‘hello world‘.zfill(20) ‘000000000hello world‘ >>> ‘hello world‘.zfill(5) ‘hello world‘ >>> ‘%%%h%ello world‘.lstrip(‘%h‘) ‘ello world‘
>>> ‘hello%world‘.partition(‘%‘) (‘hello‘, ‘%‘, ‘world‘) >>> ‘hello world‘.partition(‘ ‘) (‘hello‘, ‘ ‘, ‘world‘) >>> ‘hello world‘.partition(‘&‘) (‘hello world‘, ‘‘, ‘‘)
#ignore
str.replace(old, new[, max]) >>> ‘hello world‘.replace(‘o‘,‘x‘) ‘hellx wxrld‘ >>> ‘hello world‘.replace(‘o‘,‘x‘,1) ‘hellx world‘ >>> ‘hello world‘.replace(‘o‘,‘x‘,3) ‘hellx wxrld‘
str.rfind(str, beg=0 end=len(string)) >>> ‘hello world‘.rfind(‘o‘) 7 >>> ‘hello world‘.rfind(‘x‘) -1 >>> ‘hello world‘.rfind(‘o‘,0,5) 4
str.rindex(str, beg=0 end=len(string)) >>> ‘hello world‘.rindex(‘o‘) 7 >>> ‘hello world‘.rindex(‘x‘) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found >>> ‘hello world‘.rindex(‘o‘,0,5) 4
str.split(str="", num=string.count(str)). 参数 str -- 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。 num -- 分割次数 >>> ‘hello world‘.split() [‘hello‘, ‘world‘] >>> ‘h e l l o w\n orl d‘.split() [‘h‘, ‘e‘, ‘l‘, ‘l‘, ‘o‘, ‘w‘, ‘orl‘, ‘d‘] >>> ‘hello world‘.split(‘lo w‘) [‘hel‘, ‘orld‘]
#按照行(‘\r‘, ‘\r\n‘, \n‘)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符
>>> ‘hel\rl\no w\n\norld‘.splitlines() [‘hel‘, ‘l‘, ‘o w‘, ‘‘, ‘orld‘] >>> ‘hel\rl\no w\n\norld‘.splitlines(True) [‘hel\r‘, ‘l\n‘, ‘o w\n‘, ‘\n‘, ‘orld‘]
str.startswith(str, beg=0,end=len(string)); >>> ‘hello world‘.startswith(‘he‘) True >>> ‘hello world‘.startswith(‘hea‘) False >>> ‘ hello world‘.startswith(‘hea‘) False >>> ‘ hello world‘.startswith(‘he‘) False
>>> ‘ hello world ‘.strip(‘ hd‘) ‘ello worl‘ >>> ‘ hello world ‘.strip(‘ hdl‘) ‘ello wor‘
>>> ‘Hello World‘.swapcase() ‘hELLO wORLD‘
>>> ‘hello world‘.title() ‘Hello World‘ >>> ‘ hello world‘.title() ‘ Hello World‘
注意:定义一个十进制字符串,只需要在字符串前添加 ‘u‘ 前缀即可。 >>> u‘hello world‘.isdecimal() False >>> u‘hello2 world‘.isdecimal() False >>> u‘343434‘.isdecimal() True
str.translate(table[, deletechars]); table -- 翻译表,翻译表是通过maketrans方法转换而来。 deletechars -- 字符串中要过滤的字符列表 #!/usr/bin/python from string import maketrans # Required to call maketrans function. intab = "aeiou" outtab = "12345" trantab = maketrans(intab, outtab) str = "this is string example....wow!!!"; print str.translate(trantab, ‘xm‘); 以上实例输出结果: th3s 3s str3ng 21pl2....w4w!!!
标签:als 标题 stdin 默认 分隔符 rac port from display
原文地址:http://www.cnblogs.com/njuptlwh/p/7825035.html