index():查询子串substr第一次出现的位置,如果查找的子串不存在时,则抛出ValueError
rindex():查询子串substr最后一次出现的位置,如果查找的子串不存在时,则抛出ValueError
find():查询子串substr第一次出现的位置,如果查找的子串不存在时,则返回-1
rfind():查询子串substr最后一次出现的位置,如果查找的子串不存在时,则返回-1
```python# 查询练习s = 'hello,hello'print(s.index('lo')) # ==>3print(s.find('lo')) # ==>3print(s.rindex('lo')) # ==>9print(s.rfind('lo')) # ==>9print(s.index('k')) # ==>ValueErrorprint(s.find('k')) # ==>-1print(s.rindex('k')) # ==>ValueErrorprint(s.rfind('k')) # ==>-1```
upper():叭字符串中所有字符都转成大写字母
lower():叭字符串中所有字符都转成小写字母
swapcase():叭字符串中所有大写字母转成小写字母,小写字母转成大写字母
capitalize():叭第一个字符转换成大写,叭其余字符转换成小写
title():叭每个单词的第一个字符转换成大写,叭每个单词的剩余字符转换成小写
```python# 大小写转换练习,转换之后会产生新的字符串s = 'hello,python'a = s.upper()print(a) # ==>HELLO,PYTHONprint(s.lower()) # ==>hello,pythons1 = 'hELLo,Python'print(s1.swapcase()) # ==>HellO,pYTHONprint(s1.capitalize()) # ==>Hello,pythonprint(s1.title()) # ==>Hello,Python```
ljust(width, fillchar):左对齐,如果指定的长度小于原字符串的长度则返回原字符串。
rjust(width, fillchar):右对齐,如果指定的长度小于字符串的长度则返回原字符串。
```python# 对齐练习,空格不好区别,全用*练习s = 'hello,Pythons'# 居中print(s.center(20, '*')) # ==>***hello,Pythons****# 左对齐print(s.ljust(20, '*')) # ==>hello,Pythons*******print(s.ljust(10, '*')) # ==>hello,Pythons# 右对齐print(s.rjust(20, '*')) # ==>*******hello,Pythonsprint(s.rjust(10, '*')) # ==>hello,Pythons# 右对齐,填充字符为0print(s.zfill(20)) # ==>0000000hello,Pythonsprint(s.zfill(10)) # ==>hello,Pythonsprint('-1980'.zfill(8)) # ==>-0001980```
```python# 默认分割是空格s ='hello world python'print(s.split()) # ==>['hello', 'world', 'python']s1 ='hello|world|python'# 左边开始分割print(s1.split()) # ==>['hello|world|python']print(s1.split(sep='|')) # ==>['hello', 'world', 'python']print(s1.split(sep='|', maxsplit=1)) # ==>['hello', 'world|python']# 右边开始分割print(s1.rsplit()) # ==>['hello|world|python']print(s1.rsplit(sep='|')) # ==>['hello', 'world', 'python']print(s1.rsplit(sep='|', maxsplit=1)) # ==>['hello|world', 'python']```
isidentifier():判断指定的字符串是不是合法的标识符
isspace():判断指定的字符串是否全部由空白字符组成(回车、换行、水平制表符、空格)
isalpha():判断指定的字符串是否全部由字母组成
isdecimal():判断指定字符串是否全部由十进制的数字组成
isnumeric():判断指定字符串是否全部由数字组成
isalnum():判断指定字符串是否全部由字母和数字组成
```python# 判断练习s ='hello,python'print(s.isidentifier()) # ==>Falseprint('hello'.isidentifier()) # ==>Trueprint('张三'.isidentifier()) # ==>Trueprint('张三_123'.isidentifier()) # ==>Trueprint('\t'.isspace()) # ==>Trueprint('\n'.isspace()) # ==>Trueprint('\r'.isspace()) # ==>Trueprint(' '.isspace()) # ==>Trueprint('\b'.isspace()) # ==>Falseprint('abc'.isalpha()) # ==>Trueprint('张三'.isalpha()) # ==>Trueprint('张三1'.isalpha()) # ==>Falseprint('123'.isdecimal()) # ==>Trueprint('123四'.isdecimal()) # ==>Falseprint('ⅠⅡⅢ'.isdecimal()) # ==>Falseprint('123'.isnumeric()) # ==>Trueprint('123四'.isnumeric()) # ==>Trueprint('ⅠⅡⅢ'.isnumeric()) # ==>Trueprint('abc1'.isalnum()) # ==>Trueprint('张三123'.isalnum()) # ==>Trueprint('abc!'.isalnum()) # ==>False```
old -- 指定被替换的子串
new -- 指定替换子串的字符串
count -- 指定最大替换次数
```python# 替换练习s = 'hello,Python'print(s.replace('Python', 'java')) # ==>hello,javas = 'hello,Python,Python,Python'print(s.replace('Python', 'java', 2)) # ==>hello,java,java,Python```
```pythonlis = ['hello', 'java', 'python']print('|'.join(lis)) # ==>hello|java|pythonprint('_'.join(lis)) # ==>hello_java_pythont = ('hello', 'java', 'python')print('|'.join(t)) # ==>hello|java|pythonprint('_'.join(t)) # ==>hello_java_pythonprint('*'.join('python')) # ==>p*y*t*h*o*n```
```pythons = 'hello,python'# 没有起始位置,从0开始s1 = s[:5]print(s1) # ==>hello# 没有指定结束位置,直接切到结束s2 = s[6:]print(s2) # ==>pythons3 = '!'new_s = s1 + s2 + s3print(new_s) # ==>hellopython!# 从1开始切到5(不包括5),步长为1print(s[1:5:1]) # ==>ello# 没哟开始和结束,默认从0开始到字符串的最后一个元素print(s[::2]) # ==>hlopto# 默认从字符串的最后一个元素开始,到字符串的第一个元素结束,因为步长为负数print(s[::-1]) # ==>nohtyp,ollehprint(s[-6::1]) # ==>python```