字符串常见方法(english)

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’

猜你喜欢

转载自blog.csdn.net/newdas123/article/details/78137851
今日推荐