python_str

来源:互联网 发布:取英文名软件 编辑:程序博客网 时间:2024/06/03 20:42

字符串的常用函数

capitalize(self)S.capitalize() -> string返回一个字符串的copy,并且将第一个字母大写s = "keep on "print (s.capitalize())result > Keep on 
center(self, width, fillchar=None)S.center(width[, fillchar]) -> string返回一个总长度为指定长度的字符串,并且将S居中,左右填充指定字符,默认为空格print (s.center(10,"*"))result > ***keep***
count(self, sub, start=None, end=None)S.count(sub[, start[, end]]) -> int查询指定的子字符串的个数,可以指定开始,结束位置print (s.count("e",0,2))result > 1
endswith(self, suffix, start=None, end=None)S.endswith(suffix[, start[, end]]) -> bool返回一个布尔类型,判断在指定长度内,是不是以某个标识结尾 print (s.endswith("*")) result > false
expandtabs(self, tabsize=None)S.expandtabs([tabsize]) -> string返回一个字符串的copy,并将其中的tab转化为空格,默认是8个空格 s = "keep\ton"print (s.expandtabs(4))result >keep    on
def find(self, sub, start=None, end=None)S.find(sub [,start [,end]]) -> int返回最开始的指定字符串的indexs="keep"print (s.find("e",0,4))result > 1
format(self, *args, **kwargs)  S.format(*args, **kwargs) -> string  字符串格式化  Return a formatted version of S, using substitutions from args and kwargs.        The substitutions are identified by braces ('{' and '}')s="hello,{0},hi,{1}"print (s.format("tom","jony"))result >hello,tom,hi,jony
 def isalnum(self) S.isalnum() -> bool 判断这个字符串是不是数字或者字母
def isalpha(self)S.isalpha() -> bool判断是不是字母def isdigit(self)判断是不是数字def islower(self)判断是不是小写 def isspace(self)判断是不是空格def istitle(self)判断是不是标题(标题的单词第一个字母大写)def isupper(self)判断是不是大写
def join(self, iterable)S.join(iterable) -> string将S作用于可迭代对象,将他们使用S链接起来li = ["a" ,"b"]s = "and"print (s.join(li))result > aandb
def ljust(self, width, fillchar=None)S.ljust(width[, fillchar]) -> string字符串左对齐,右侧填充s = "test"print (s.ljust(10,"*"))result > test******
def lower(self)def upper(self)
def lstrip(self, chars=None)S.lstrip([chars]) -> string or unicode去掉左空格
def partition(self, sep)S.partition(sep) -> (head, sep, tail)找到S中的分隔符,返回前面的,自己,后边的print (s.partition("e"))result > ('t', 'e', 'st')
def replace(self, old, new, count=None)S.replace(old, new[, count]) -> string返回一个字符串的copy ,并用新字符串替换旧的print (s.replace("e","L",1))result >tLst 
def rsplit(self, sep=None, maxsplit=None)S.rsplit([sep [,maxsplit]]) -> list of stringsprint (s.split("e",2))result >  ['t', 'st']
def splitlines(self, keepends=False)S.splitlines(keepends=False) -> list of strings换行分割s = "test\nheelo"print (s.splitlines())result > ['test', 'heelo']
def startswith(self, prefix, start=None, end=None)S.startswith(prefix[, start[, end]]) -> bool
 def swapcase(self) S.swapcase() -> string 大写换小写,小些转大写
 def title(self) S.title() -> string 将字符串转换为标题
原创粉丝点击