字符串常见方法(english)

来源:互联网 发布:ipad淘宝hd5.0.1版本 编辑:程序博客网 时间:2024/06/05 17:45

string normal method

find rfind

return first index or -1
mystr.find(str, start=0, end=len(mystr))

index rindex

Similar to find, if not in str there will raise a error
mystr.index(str, start=0, end=len(mystr))

count

return times
mystr.count(str, start=0, end=len(mystr))

replace

not more than count
mystr.replace(str1, str2, mystr.count(str1))

split

mystr.split(‘/’, maxsplit)

capitalize

upper first character of string
mystr.capitalize()

title()

upper fisrt character of every word in string
mystr.title()

startswith

return True or False
mystr.startswith(’s’)

endswith

return True or False
mystr.endswith(’s’)

lower()

lower every character in string
mystr.lower()

upper()

lower every character in string
mystr.lower()

ljust rjust center

in: ‘liuda’.ljust(10,’b’)
out: ‘liudabbbbb’

lstrip rstrip strip

in: ‘liudaliuda’.strip(‘liud’)
out: ‘aliuda’

partition rpatition

return tuple (before_str str behind_str)
in: ‘goodliudaliudagood’.partition(‘liuda’)
out: (‘good’, ‘liuda’, ‘liudagood’)
in: ‘goodliudaliudagood’.rpartition(‘liuda’)
out: (‘goodliuda’, ‘liuda’, ‘good’)

splitlines()

return a list of every line
mystr.splitlines()

isalpha() isdigit() isalnum() isspace()

alpha digit alpha or digit space

join

in: ‘-‘.join([‘my’, ‘name’, ‘is’, ‘liuda’])
out: ‘my-name-is-liuda’

原创粉丝点击