学习20--字符串的常用操作方法及判断
四、常用操作方法
字符串的常用操作方法有查找、修改、和判断三大类
第一点 记单词 记住这个函数的名字
第二点 记住函数的作用
第三点 记住函数的参数传递方式
1.查找 2.修改 3.判断
注意: 在修改原字符串时会生成一个新字符串,必须使用一个序列接受修改的值.
说明: 原有的字符串不会且不可改变
数据是否改变划分为:
可变类型
不可变类型: 所有进行 '修改' 的单词都为不可变类型.
1、查找:
① find() 从左到右
② index() 从左到右
③ count() 出现的次数
④ rindex()从右侧开始
⑤ rfind() 从右侧开始
1.1 查找 find()
所谓字符串查找方法既是查找子串在字符串中的位置或出现的次数
1.1.1 .find():检测某个子串是否包含在这个字符串中,
如果在返回这个 子串 开始的位置下标,否则则返回-1
.find(三个参数)语法:
字符串序列.find('子串',开始位置下标,结束位置下标)
1.1.2 .index():检测某个字符串是否包含在这个字符串中,
如果在则返回这个子串的开始位置下标 否则则报异常
.index(三个参数) 语法:
字符串序列.index('子串',开始位置下标,结束位置下标)
1.1.3 .count():检测出现的次数,有几次出现几个次数 没有包含返回0.
注意:
① 所谓子串是字符串当中的一部分字符
② 开始和结束位置可以省略,表示在整个字符串序列中查找
快速体验:
分别使用
.find(三个参数):
.index(三个参数)
.count(子串): 返回某个子串在字符串中出现的次数
.rfind(三个参数): 和find()功能相同,但查找方向为 右侧 开始
.rindex(三个参数): 和index()功能相同,但查找方向为 右侧 开始
1.1.1 .find()
mystr = "hello world and itcast and itchengxuyuan and Python"
1. find()语法: 字符串序列.find(子串,开始位置下标,结束位置下标)
print(mystr.find('and')) # 12 从左往右,第一次出现位置的下标
print(mystr.find('and',15,30)) # 23 从左往右,第一次出现位置的下标
print(mystr.find('ands')) # -1 ands子串不存在
print('---------------------------------------')
2 count()
print(mystr.count('and')) # 3
print(mystr.count('and',15,30)) # 1
print(mystr.count('ands')) # 0 没有包含返回0
print('---------------------------------------')
3.index()语法: 字符串序列.index(子串,开始位置下标,结束位置下标)
print(mystr.index('and')) # 12
print(mystr.index('and',15,30)) #23
print(mystr.index('ands')) # 如果index查找子串不存在,报错
print('-----------------------------')
4.rfind()
print(mystr.rfind('and')) # 41
5. rindex()
print(mystr.rindex('ands')) # 报错
2、修改:
重点
replace---替换
split ---分隔
join ---合并
非重点
Capitalize() ---只有第一个字符串换成大写
title() ---每个单词首字母变成大写
lower() ---大写转小写
upper() ---小写转大写
方向删除
strip() --- 删除两侧空白
lstrip() --- 删除左侧空白
rstrip() --- 删除右侧空白
方向对齐
ljust() --- 字符串居 左 对齐
rjust() --- 字符串居 右 对齐
center()--- 字符串居中对齐
2 修改
所谓修改字符串,指的就是通过函数的形式修改字符串中的数据
2.2.1 replace():替换
语法: 字符串序列.replace('子串','新子串',替换次数)
注意: 替换次数如果查出子串出现次数,则替换次数为该子串出现次数.
说明: replace()--函数有返回值,返回值是修改后的字符串
调用了replace函数后,发现原有字符串的数据并没有做到修改
修改后的数据是replace函数的返回值
说明: 字符串是不可变数据类型.
数据是否可以改变划分为 '可变类型' 和 '不可变类型'
2.2.2 split():分割
语法: new_str = 字符串序列.split('分割字符',num)
注意:
① num 表示的是分割字符串字符出现的次数
即将来返回数据个数为 num+1
② 如果分割字符是原有字符串的子串,分割后则丢失该子串
2.2.3 join(): 合并
语法: new_str = '连接的字符或者子串'.join(多字符串组成的序列)
快速分别体验:
① replace()替换,
② split()分割,
③ join()合并
2.2.1 replace()替换--函数有返回值,返回值是修改后的字符串
mystr = "hello world and itcast and itchengxuyuan and Python"
new_mystr = mystr.replace("and","he")
print(mystr.replace('and','he')) # hello world he itcast he itchengxuyuan he Python
print(mystr.replace('and','he',1)) # hello world he itcast and itchengxuyuan and Python
超出替换次数会报错
print(mystr)
print(new_mystr)
********调用了replace函数后,发现原有字符串的数据并没有做到修改
修改后的数据是replace函数的返回值
说明: 字符串是不可变数据类型.
数据是否可以改变划分为 '可变类型' 和 '不可变类型'
说明replace 是不可变类型
print('--------------------------------------------------')
2.2.2 split() 分割--返回一个列表
mystr = "hello world and itcast and itchengxuyuan and Python"
list1 = mystr.split('and')
print(list1) # 如果分割字符是原有字符串的子串,分割后则丢失该子串
list1 = mystr.split('and',2)
print(list1) # num 表示的是分割字符串字符出现的次数, 即将来返回数据个数为 num+1
print('--------------------------------------------------')
2.2.3 join() 合并--合并列表里面的字符串数据为一个大字符串
mylist1 = ['aa','bb','cc']
aa...bb...cc
new_str = '...'.join(mylist1)
print(new_str) # aa...bb...cc
2.2.4 字母大小写转换
① capitalize():将字符串第一个字符转换成大写
语法: 字符串序列.capitalize
② title(): 将每个单词首字母转换成大写
语法: 字符串序列.tile()
③ lower(): 将字符串中大写转换成小写
语法: 字符串序列.lower()
④ upper(): 将字符串中小写转换成大写
语法: 字符串序列.upper()
快速分别体现:
capitalize()
title()
lower()
upper()
mystr = "hello world and itcast and itchengxuyuan and Python"
① capitalize() 将字符串第一个字符转换成大写
print(mystr.capitalize())
new_str1 = mystr.title()
print(new_str1)
print('----------分割线----------------')
② title() 将每个单词首字母转换成大写
print(mystr.title())
new_str2 = mystr.title()
print(new_str2)
print('----------分割线----------------')
③ lower() 将字符串中大写转换成小写
print(mystr.lower())
new_str3 = mystr.lower()
print(new_str3)
print('----------分割线----------------')
④ upper() 将字符串中小写转换成大写
print(mystr.upper())
new_str4 = mystr.upper()
print(new_str4)
"""
2.2.5 strip() 删除空白字符
语法: 字符串序列.strip()
① strip(): 删除字符串 两侧 空白字符
② lstrip():删除字符串 左边 空白字符
③ rstrip():删除字符串 右边 空白字符
"""
快速体验
mystr = " hello world and itcast and itchengxuyuan and Python "
① strip(): 删除字符串 两侧 空白字符
new_str1 = mystr.strip()
print(new_str1)
print('-----------------分割线-----------------')
② lstrip():删除字符串 左边 空白字符
new_str2 = mystr.lstrip()
print(new_str2)
print('-----------------分割线-----------------')
③ rstrip():删除字符串 右边 空白字符
new_str3 = mystr.rstrip()
print(new_str3)
"""
2.2.6 左中右对齐函数
语法: 字符串序列.函数名(长度,'填充字符')
① ljust(): 语法: 字符串序列.ljust(长度,'填充字符')
返回一个原字符串左边对齐,并使用指定字符串(默认空格)填充至对应长度的新字符串
② rjust(): 语法: 字符串序列.rjust(长度,'填充字符')
返回一个原字符串右边对齐,并使用指定字符串(默认空格)填充至对应长度的新字符串
③ center():语法: 字符串序列.center(长度,'填充字符')
返回一个原字符串中间对齐,并使用指定字符串(默认空格)填充至对应长度的新字符串
快速体验
"""
mystr = 'hello world'
① ljust()语法: 字符串序列.ljust(长度,'填充字符')
new_str1 = mystr.ljust(20,'*')
print(new_str1)
print('--------------------分割线-------------------')
② rjust()语法: 字符串序列.rjust(长度,'填充字符')
new_str2 = mystr.rjust(20,'*')
print(new_str2)
print('--------------------分割线-------------------')
③ center()语法: 字符串序列.center(长度,'填充字符')
new_str3 = mystr.center(20,'*')
print(new_str3)
3、判断: 以is开头
① startswith() 是否以指定字符串开头,实则返回 True;否则返回 False
② isalpha() 是否全部是 字母 组成
③ isdigit() 是否全部是 数字 组成
④ isalnum() 非空字符是否以数字或字母组成
⑥ isspace() 是否全部是空格(空白组成)
- 判断
所谓判断既是判断真假,返回的结果是布尔型数据类型
True 或 False
① startswith(): 检查字符串是否以指定子串开头,是则返回True,否则返回False.
如果设置开始和结束位置下标,则在指定范围内检查.
语法: 字符串序列.starswith('子串',开始位置下标,结束位置下标)
② endswith(): 检查字符串是否以指定子串开头,是则返回True,否则返回False.
如果设置开始和结束位置下标,则在指定范围内检查.
语法: 字符串序列.endswith('子串',开始位置下标,结束位置下标)
快速体验:
starswith():
endswith():
mystr = "hello world and itcast and itchengxuyuan and Python"
① starswith()语法: 字符串序列.starswith('子串',开始位置下标,结束位置下标)
print(mystr.startswith('hello'))
new_str1 = mystr.startswith('hello') # 开始和结束位置下标可以省略
print(new_str1)
new_str1 = mystr.startswith('ello')
print(new_str1)
print('-----------------分割线-----------------')
② endswith()语法: 字符串序列.endswith('子串',开始位置下标,结束位置下标)
new_str2 = mystr.endswith('Python')
print(new_str2)
new_str2 = mystr.endswith('python')
print(new_str2)
"""
③ isalpha(): 如果字符串至少有一个字符并且所有字符都是字母则返回True;否则返回False
④ isdigit(): 如果字符串只包含数字,则返回True;否则返回False
⑤ isalnum(): 如果字符串中至少有一个字符,并且所有字符都是字母或数字组合,返回True;否则False
⑥ isspace(): 如果字符串中只含有空白,则返回True;否则False
"""
mystr = "hello world and itcast and itchengxuyuan and Python"
③ isalpha(): 至少有一个字符并且所有字符都是字母
print(mystr.isalpha())
print('-----------------分割线-----------------')
④ isdigit():只包含数字,
print(mystr.isdigit())
mystr1 = '123456'
print(mystr1.isdigit())
print('-----------------分割线-----------------')
⑤ isalnum():至少有一个字符,并且所有字符都是字母或数字组合
print(mystr.isdigit())
mystr2 = 'abc1234'
print(mystr2.isalnum())
print('-----------------分割线-----------------')
⑥ isspace():含有空白/空格
print(mystr.isspace())
mystr3 = ' '
print(mystr3.isspace())
总结
"""
下标
计算机为数据序列中每个元素分配的从0开始
切片
序列名[开始位置:结束位置:步长]
常用操作方法
find()--查找
index()--查找
count()--计数
rfind()
rindex()
"""

浙公网安备 33010602011771号