如下学习python的字符串用法。
print(dir(str))
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill' ]如上是字符串的所有方法,一个一个看,共44个
1.返回第一个字母大写capitalize(...)
capitalize()->string
>>>a = 'hello world'
>>> b = a.capitalize()
>>> print ( b)
Hello world 2.按指定长度填充特定字符center(...)
S.center(width[,fillchar])->string >>> a ='linux' >>> printa.center(7,'h') hlinuxh >>> printa.center(8,'h') hlinuxhh >>> printa.center(9,'h') hhlinuxhh 3.查找某字符串出现的次数count(...)
S.count(sub[,start[,end]])->int >>> a ="this is my dog, i love this dog and it's a good dog!" >>> printa.count('dog') 3 >>> printa.count('dog',15) 2 >>> printa.count('dog',15,30) 0 >>> printa.count('dog',15,32) 1 4.以指定的编码格式解码字符串。默认编码为字符串编码(适合 python2中处理中文)decode(...)
S.decode([encoding[,errors]])->object b ='strid' >>> b.decode('utf-8') u'strid' 5.用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回 True,否则返回 False。可选参数 "start"与 "end"为检索字符串的开始与结束位置endswith(...)
S.endswith(suffix[,start[,end]])->bool >>> shaw ='I am shaw,what\'s your name ?' >>> shaw.endswith('?') True >>> shaw.endswith('w',7,9) True >>> shaw.endswith('w',7,8) False 6.把字符串中的 tab 符号 ('\t')转为空格, tab 符号 ('\t')默认的空格数是 8, tabsize-- 指定转换字符串中的 tab 符号 ('\t')转为空格的字符数。expandtabs(...)
S.expandtabs([tabsize])->string >>> info ='today is a good d\tay' >>> printinfo.expandtabs() today isa good d ay >>> printinfo.expandtabs(4)# 把tab装换成4个空格 today isa good d ay >>> printinfo.expandtabs(1) today isa good d ay# 把tab装换成1个空格 7.检测字符串中是否包含子字符串 str ,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,如果包含子字符串,则返回开始的索引值,否则返回 -1。find(...)
S.find(sub[,start[,end]])->int >>> a ='stivenwang' >>> a.find('w') 6 >>> a.find('w',9) -1 >>> a.find('w',9,11) -1 8.格式换字符串输出(方法与 %相似,但可以指定顺序)format(...)
S.format(*args,**kwargs)->string
>>> name ='StivenWang' >>> fruit ='apple' >>> print'my name is {},I like {}'.format(name,fruit) my name isStivenWang,I like apple >>> print'my name is {1},I like {0}'.format(fruit,name) my name isStivenWang,I like apple >>> print'my name is {mingzi},I like{shuiguo}'.format(shuiguo=fruit,mingzi=name) my name isStivenWang,I like apple9.检测字符串 string中是否包含子字符串 str ,如果存在,则返回 str在 string中的索引值,如果指定 beg(开始)和 end(结束)范围,则检查是否包含在指定范围内,该方法与 pythonfind()方法一样,只不过如果 str不在 string中会报一个异常 (ValueError:substringnotfound)。
index(...)
S.index(sub[,start[,end]])->int >>> str1 ="this is string example....wow!!!" >>> str2 ="exam" >>> printstr1.index(str2) 15 >>> printstr1.index(str2,20) Traceback (most recent call last): File"<input>",line1,in<module> ValueError: substringnot found报错 10.检测字符串是否由字母或数字组成。isalnum(...)
S.isalnum()->bool
>>> a ='123' >>> a.isalnum() True >>> b ='shaw' >>> b.isalnum() True >>> c ='shaw123' >>> c.isalnum() True >>> d ='th 123' >>> d.isalnum() False 11.检测字符串是否只由字母组成isalpha(...)
S.isalpha