目录
1 find
str.find(xxx, start=0, end=len(str))
检测xxx是否包含在str中,如果是返回开始的索引值,否则返回-1
>>> str = "you are a beautiful girl"
>>> str.find('are',0,len(str))
4
2 index
跟find()方法一样,只不过如果str不在 str中会报一个异常.
str.index(xxx, start=0, end=len(str))
>>> str = "you are a beautiful girl"
>>> str.index('are',0,len(str))
4
3 count
返回 xxx在start和end之间 在 str里面出现的次数
str.count(xxx, start=0, end=len(str))
>>> str.count("a",0,len(str))
3
4 replace
把 str 中的 xxx1 替换成 xxx2,如果 count 指定,则替换不超过 count 次.
str.replace(xxx1, xxx2, str.count(xxx1))
>>> str.replace('a','A',2)
'you Are A beautiful girl'
5 split
以 xxx 为分隔符切片 str,如果 maxsplit有指定值,则仅分隔 maxsplit 个子字符串
str.split(xxx=" ", 2)
>>> str.split(" ")
['you', 'are', 'a', 'beautiful', 'girl']
>>> str.split(" ", 3)
['you', 'are', 'a', 'beautiful girl']
6 capitalize
把字符串的第一个字符大写
str.capitalize()
>>> str.capitalize()
'You are a beautiful girl'
7 title
把字符串的每个单词首字母大写
>>> str.title()
'You Are A Beautiful Girl'
8 startswith
检查字符串是否是以 xxx 开头, 是则返回 True,否则返回 False , str.startswith("xxx")
endswith 检查字符串是否以xxx结束,如果是返回True,否则返回 False, str.endswith("xxx")
>>> str.startswith('yo')
True
>>> str.startswith('aa')
False
>>> str.endswith('girl')
True
9 lower、upper
lower 转换 str 中所有大写字符为小写 str.lower()
upper 转换 str 中的小写字母为大写 str.upper()
>>> aa="HaHaHa"
>>> aa.lower()
'hahaha'
>>> aa.upper()
'HAHAHA'
10 ljust、rjust、center
ljust 返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串 str.ljust(width)
rjust 返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串 str.rjust(width)
center 返回一个原字符串居中,并使用空格填充至长度 width 的新字符串 str.center(width)
>>> str.ljust(30)
'you are a beautiful girl '
>>> str.rjust(30)
' you are a beautiful girl'
>>> str.center(30)
' you are a beautiful girl '
11 lstrip、strip、strip
lstrip 删除 str 左边的空白字符 str.
lstrip()
rstrip 删除 str 字符串末尾的空白字符 str.
rstrip()
strip 删除str字符串两端的空白字符 str.strip()
>>> aa = " you are perfect "
>>> aa.lstrip()
'you are perfect '
>>> aa.rstrip()
' you are perfect'
>>> aa.strip()
'you are perfect'
12 rfind
类似于 find()函数,不过是从右边开始查找.
str.rfind(xxx, start=0,end=len(str) )
>>> cc="you are perfect and are you"
>>> cc.rfind('are',0,len(cc))
20
>>> cc.find('are',0,len(cc))
4
13 rindex
类似于 index(),不过是从右边开始.
str.rindex( xxx, start=0,end=len(str))
cc.index("aa",0,len(cc))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
>>> cc.index("are",0,len(cc))
4
>>> cc.rindex("are",0,len(cc))
20
14 partition
把str以xxx分割成三部分,xxx前,xxx和xxx后
str.partition(xxx)
>>> cc.partition('perfect')
('you are ', 'perfect', ' and are you')
15 rpartition
类似于 partition()函数,不过是从右边开始.
str.rpartition(xxx)
>>> Q="a boy like to the girl and girlrl"
>>> Q.partition('girl')
('a boy like to the ', 'girl', ' and girlrl')
>>> Q.rpartition('girl')
('a boy like to the girl and ', 'girl', 'rl')
16 splitlines
按照行分隔,返回一个包含各行作为元素的列表
str.splitlines()
>>> dd="you are perfect\nand\nare you"
>>> print(dd)
you are perfect
and
are you
>>> dd.splitlines()
['you are perfect', 'and', 'are you']
17 isalpha
如果 str 所有字符都是字母 则返回 True,否则返回 False
str.isalpha()
>>> A="hello"
>>> B="123heLL0123"
>>> A.isalpha()
True
>>> B.isalpha()
False
18 isdigit
如果 str 只包含数字则返回 True 否则返回 False.
str.isdigit()
>>> B="123heLL0123"
>>> C='1234'
>>> C.isdigit()
True
>>> B.isdigit()
False
19 isalnum
如果 str 所有字符都是字母或数字则返回 True,否则返回 False
str.isalnum()
>>> A="hello"
>>> B="123heLL0123"
>>> C='1234'
>>> D="123gujh-*"
>>> A.isalnum()
True
>>> B.isalnum()
True
>>> C.isalnum()
True
>>> D.isalnum()
False
20 isspace
如果 str 中只包含空格,则返回 True,否则返回 False.
str.isspace()
>>> D="123gujh-*"
>>> E=" "
>>> E.isspace()
True
>>> D.isspace()
False
21 join
str 中每个元素前面插入xxx,构造出一个新的字符串
str.join(xxx)
>>> F="Love"
>>> G=['1','2','3']
>>> H="_"
>>> F.join(G)
'1Love2Love3'
>>> H.join(F)
'L_o_v_e'
>>> F.join(H)
'_'
>>> G.join(F)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'