Python String Methods 3,,Python lju


Python ljust()方法 --rjust())#返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串

技术分享
str.ljust(width[, fillchar])>>> ‘    hello world‘.ljust(20)‘    hello world     ‘>>> ‘    hello world‘.ljust(20,‘=‘)‘    hello world=====‘
View Code

Python lower()方法 --Python upper() #转换字符串中所有大写字符为小写

技术分享
>>> ‘HellO WorlD‘.lower()‘hello world‘>>> ‘HELLO WORLD‘.lower()‘hello world‘>>> ‘HelLO Wo123lD‘.lower()‘hello wo123ld‘
View Code

Python lstrip()方法 --Python rstrip() #删除 string 字符串末尾的指定字符(默认为空格)

技术分享
>>> ‘    hello world‘.lstrip()‘hello world‘>>> ‘%%%hello world‘.lstrip(‘%‘)‘hello world‘>>> ‘%%%h%ello world‘.lstrip(‘%‘)‘h%ello world‘
View Code

Python zfill()方法 #返回指定长度的字符串,原字符串右对齐,前面填充0

技术分享
>>> ‘hello world‘.zfill(20)‘000000000hello world‘>>> ‘hello world‘.zfill(5)‘hello world‘>>> ‘%%%h%ello world‘.lstrip(‘%h‘)‘ello world‘
View Code

Python partition() #用来根据指定的分隔符将字符串进行分割。
如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串

技术分享
>>> ‘hello%world‘.partition(‘%‘)(‘hello‘, ‘%‘, ‘world‘)>>> ‘hello world‘.partition(‘ ‘)(‘hello‘, ‘ ‘, ‘world‘)>>> ‘hello world‘.partition(‘&‘)(‘hello world‘, ‘‘, ‘‘)
View Code

Python rpartition(str) #类似于 partition()函数,不过是从右边开始查找

#ignore

Python replace() #把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次

技术分享
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‘
View Code

Python rfind()方法 #返回字符串最后一次出现的位置(从右向左查询),如果没有匹配项则返回-1

技术分享
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
View Code

Python rindex()方法#返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间

技术分享
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
View Code

Python split()方法 #通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串

技术分享
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‘]
View Code

Python splitlines()方法

#按照行(‘\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‘]
View Code

Python startswith()方法 #用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查

技术分享
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
View Code

Python strip()方法 #用于移除字符串头尾指定的字符(默认为空格) (在 string 上执行 lstrip()和 rstrip())

技术分享
>>> ‘  hello world    ‘.strip(‘ hd‘)‘ello worl‘>>> ‘  hello world    ‘.strip(‘ hdl‘)‘ello wor‘
View Code

Python swapcase()方法 #用于对字符串的大小写字母进行转换

技术分享
>>> ‘Hello World‘.swapcase()‘hELLO wORLD‘
View Code

Python title()方法 #返回"标题化"的字符串,就是说所有单词都是以大写开始,其余字母均为小写(见 istitle())

技术分享
>>> ‘hello world‘.title()‘Hello World‘>>> ‘ hello world‘.title()‘ Hello World‘
View Code

Python isdecimal()方法 #Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。

技术分享
注意:定义一个十进制字符串,只需要在字符串前添加 ‘u‘ 前缀即可。>>> u‘hello world‘.isdecimal()False>>> u‘hello2 world‘.isdecimal()False>>> u‘343434‘.isdecimal()True
View Code

Python translate()方法 #根据参数table给出的表(包含 256 个字符)转换字符串的字符, 要过滤掉的字符放到 del 参数中

技术分享
str.translate(table[, deletechars]);table -- 翻译表,翻译表是通过maketrans方法转换而来。deletechars -- 字符串中要过滤的字符列表#!/usr/bin/pythonfrom 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!!!
View Code

Python String Methods 3

评论关闭