直接上代码: 常用的方法
find: 字符串获取对应索引
str = '0123456789' # 为了方便查看对应的索引,此处显示索引字符串
str.find('2') # 2
str.find('5') # 5
str.find('a') # -1 不存在返回-1index:返回索引值,不存在的会报错
说明: 跟find()方法一样,只不过如果str不在字符串变量中会报一个异常
第一个参数:需要查找的字符串片段
第二个参数:Lindex从左边开始查找
第三个参数:Rindex从右边开始查找
例子: str = 'hello world!!!'
print(str.index('O1', 0, 10)) // 不存在的话: Traceback (most recent call last)
print(str.index('O', 0, 10)) // 4 存在就返回索引值count:返回指定字符串片段的出现次数
说明: 返回str在start和end之间 在 mystr里面出现的次数
例子: str = '1232521212152152'
print(str.count('2')) # 7 存在的话返回次数
print(str.count('7')) # 0 不存在的换返回0replace:替换字符串中的指定字符片段
把mystr中的str1替换成str2,如果count指定,则替换不超过count次.
例子: mystr.replace(str1, str2, mystr.count(str1))
print(mystr.replace('python','php')) # 结果:hello world php and phpchina
print(mystr.replace('python','php',1)) # 只替换一次,结果:hello world php and pythonchinasplit:以str为分隔符切片mystr
以str为分隔符切片mystr,如果maxsplit有指定值,则仅分隔maxsplit个子字符串,返回的是一个列表
例子:
split格式:mystr.split(str="str", maxsplit=maxsplit)
print(mystr.split(' ')) # 以空格切割整个字符串,结果:['hello', 'world', 'python', 'and', 'pythonchina']
print(mystr.split(' ', 2)) # 以空格切割整个字符串,只切割两次,结果:['hello', 'world', 'python and pythonchina']join:mystr 中每个元素后面插入str,构造出一个新的字符串
join格式:str.join(mystr) # 前面插入到后面字符串
print('_'.join('hello')) # 把1插入到hello的每个元素后面,输出结果为:h_e_l_l_o
print('_'.join(['hello', 'world', 'hello', 'python'])) # 输出结果为:hello_world_hello_pythoncapitalize:把字符串的第一个字符大写
# 整个字符串的第一个字母大写
print(mystr.capitalize()) # 输出结果为:Hello world python and pythonchinatitle:把字符串的每个单词首字母大写
# 字符串中每个单词首字母大写
print(mystr.title()) # 输出结果为:Hello World Python And Pythonchinastartswith:检查字符串是否是以str开头, 是则返回True,否则返回False,相对应的是endswith,以str结尾
mystr = 'hello world python and pythonchina'
print(mystr.startswith('hello')) # 结果为True
print(mystr.endswith('hello')) # 结果为Falselower:转换 mystr 中所有大写字符为小写。对应的是upper,把全部字符转换成大写
mystr = 'Hello Python'
print(mystr.lower()) # 结果hello python
print(mystr.upper()) # 结果HELLO PYTHONljust:返回一个原字符串左对齐,并使用空格填充至字符长度width的新字符串。对应的是rjust,center
mystr = 'Hello Python'
print(mystr.ljust(20)) # 结果hello python
print(mystr.rjust(20)) # 结果 hello python
print(mystr.center(20)) # 结果 hello pythonstrip:删除mystr头尾空白字符,对应的(左)lstrip,对应的(右)rstrip
mystr = ' hello '
print(mystr.strip()) #结果:hello
print(mystr.lstrip()) #结果:hello
print(mystr.rstrip()) #结果: hellopartition:把mystr以str分割成三部分,str前,str和str后,返回一个元组。对应的是rpartition
mystr = 'hello world hello china'
print(mystr.partition('world')) # 结果('hello ', 'world', ' hello china')
print(mystr.rpartition('hello')) # 结果('hello world ', 'hello', ' china')splitlines:按照行分隔,返回一个包含各行作为元素的列表
mystr = 'hello world\nhello china'
print(mystr.splitlines()) # 结果:['hello world', 'hello china']isalpha:判断mystr是否都是字母,是则返回 True,否则返回 False
mystr = 'hello world'
print(mystr.isalpha()) # 有空格,结果:Falseisdigit:判断mystr是否都是数字 是则返回 True,否则返回 False
mystr = '888'
print(mystr.isdigit()) # 结果:Trueisalnum:判断mystr是否都是数字或者,是则返回 True,否则返回 False
mystr = '888abc'
print(mystr.isalnum()) # 结果:Trueisspace:判断mystr是否只包含空格,是则返回 True,否则返回 False
mystr = ' '
print(mystr.isspace()) # 结果:True字符串剪切指定长度
str = '0123456789'print(str[1:3]) #截取第一位到第三位的字符 12print(str[:]) #截取字符串的全部字符 0123456789print(str[6:]) #截取第七个字符到结尾 6789print(str[:-3]) #截取从头开始到倒数第三个字符之前 0123456print(str[2]) #截取第三个字符 2print(str[-1]) #截取倒数第一个字符 9print(str[::-1]) #创造一个与原字符串顺序相反的字符串 9876543210print(str[-3:-1]) #截取倒数第三位与倒数第一位之前的字符 78print(str[-3:]) #截取倒数第三位到结尾 789print(str[:-5:-3]) # 逆序截取 96
















