mystr = 'hello world! hello Hefei'
print(mystr)
图1
1 增
str1 = 'my'
str2 = 'name'
str_com = '%s %s'%(str1,str2)
str_com
图2
str1 = 'my'
str2 = 'name'
str_com = str1 ' ' str2
str_com
图3
str1 = 'my'
str2 = 'name'
str_com = '{} {}'.format(str1,str2)
str_com
图4
str1 = 'my'
str2 = 'name'
str_com = f'{str1} {str2}' # 3.6
str_com
图5
1.1 join():语法:str.join(mystr) : 在mystr中每个元素后面增加str,构造出一个新的字符串
str1 = ' '
str2 = 'name'
str1.join(str2)
图6
2 删2.1 lstrip :删除字符串左边的空白字符
mystr = ' hello'
mystr.lstrip()
图7
2.2 rstrip : 删除字符串右边的空白字符
mystr = 'hello '
mystr.rstrip()
图8
2.3 strip : 删除字符两边的空白字符
mystr = ' hello '
mystr.strip()
图9
my_str = ' hello '
my_str.strip(' ')
图10
3 改3.1 replace语法: mystr.replace(str1,str2)
my_str = 'hello'
my_str.replace('l','x')
图11
3.2 split()语法:mystr.split(str1)
mystr= '小明, 小红, 小白'
mystr.split(', ')
图12
3.3- capitalize():首字母大写
- title():每个单词首字母大写
3.4 mystr.upper(): 把所有字符中的小写字母转换成大写字母3.5 mystr.lower(): 把所有字符中的大写字母转换成小写字母
my_str = 'hello world'
my_str.capitalize()
图13
my_str = 'hello world'
my_str.title()
图14
my_str = 'hello world'
my_str.upper()
图15
my_str = 'Hello world'
my_str.lower()
图16
4 查找功能4.1 find : 返回查找对象的在索引内容中,则返回索引值,否则返回为-1 语法规则 : mystr.find(str, start=0, end=len(mystr))- 第一个参数为查找的对象
- 第二个参数为查找的起始索引位置
- 第三个参数为查找的终止索引位置,可防止索引超出范围,导致Error
mystr = 'hello world! hello Hefei'
mystr.find('Hefei',0,len(mystr))
# mystr.find('Hefei',0,18)
图17
4.2 index: 语法:mystr.index(str,start=0, end=len(mystr))- 第一个参数为查找的对象
- 第二个参数为查找的起始索引位置
- 第三个参数为查找的终止索引位置,可防止索引超出范围,导致Error
mystr = 'hello world! hello Hefei'
mystr.index('ab',0,len(mystr))
图18
4.3 count : 返回str在start和end之间 在mystr里面出现的次数
mystr = 'hello world! hello Hefei. I love Hefei'
mystr.count('Hefei')
图19
4.4 startswith: 检查字符串是否是以‘Hello’开头,是则返回True,否则返回 False
mystr = 'hello world! hello Hefei. I love Hefei'
mystr.startswith('hello')
图20
4.5 endswith :检查字符串是否以‘Hefei’结束,是则返回True,否则返回 False
mystr = 'hello world! hello Hefei. I love Hefei'
mystr.endswith('Hefei')
图21
4.6 rfind :类似find()函数,不过从右边开始查找
mystr = 'hello world! hello Hefei. I love Hefei'
mystr.rfind('hello')
图22
4.7 rindex: 类似于index(),不过是从右边开始
mystr = 'hello world! hello Hefei. I love Hefei'
mystr.rindex('hello')
图23
,