字符串的修改(具体函数与语法都在代码注释中)
'''
语法:字符串序列.replace(被替换的内容,新内容,替换次数)
'''
str1='hello world and hello pathon and hello backoff'
str1.replace('and','和')#替换次数不写默认全部替换,替换次数如果超出,表示替换所有
print(str1)#此时打印str1不会变
str=str1.replace('and','和')#替换后原来的字符串变为一个新的字符串,需要一个新的地址空间存储
print(str)
#注意:字符串是不可改变类型
'''
2.split()-分割字符串
语法:字符串序列.split(分割字符,分割次数)
'''
list1=str1.split('and')#返回一个列表值,丢失分割内容
print(list1)
'''
3 join()--合并一个列表里的元素为一个大字符串
语法:'连接符号(自己随意取的)'.join.(被连接的列表)
'''
list2=['aa','bb','cc']
list3='and'.join(list2)
print(list3)
删除字符串空白字符(具体函数与语法都在代码注释中)
str1=' hello world and hello pathon and hello backoff '
print(str1.lstrip())#删除左侧空白字符
print(str1.rstrip())#删除右侧空白字符
print(str1.strip())#删除两侧空白字符
'''
对齐
语法:字符串序列.just('长度','填充字符')
'''
str='hello world and hello pathon and hello backoff'
print(str.ljust(100,'.'))#左侧对齐
print(str.rjust(100,'.'))#右侧对齐
查找字符串元素(具体函数与语法都在代码注释中)
'''
find:
语法:字符串序列.find(子串,开始位置下标,结束位置下标)
注意:开始和结束的位置下标可以省略,意味在整个字符串中查找
'''
string1='hello world and hello python'
print(string1.find('and'))#12
print(string1.find('and',15,20))#-1,find函数中子串存在,返回子串下标,不存在返回-1
#注意:index函数和find函数使用方法一致,只不过当子串不存在时程序报错
string1='hello world and hello python'
print(string1.index('and'))#12
"""
count:
作用:统计次数
使用方法一致
"""
string1='hello world and hello python'
print(string1.count('and'))#查找上述字符串中的and出现次数,当子串不存在时返回0
'''
提示:
rfind与find函数的用法一致,但是查找从右边开始
rindex与index函数的用法一致,但是查找从右边开始
'''