字符串常用操作方法
- 1. 查找
- 1.1 find()
- 1.2 index()
- 1.3 count()
- 1.4 rfind()
- 1.5 rindex()
- 2.修改
- 2.1 replace() 替换
- 2.2 split() 分割
- 2.3 join() 合并
- 2.4 大小写转换(非重点)
- 2.5 删除空白字符(非重点)
- 2.6 字符串对齐(非重点)
- 3.判断
1. 查找
字符串的查找方法 即查找字串在字符串中的位置或出现的次数。
1.1 find()
1.2 index()
1.3 count()
1.4 rfind()
1.5 rindex()
"""
find() 检测某个字串是否包含在这个字符串中,
若【在】返回这个字符串开始的位置下标。
【不在】则返回-1
语法
字符串序列.find(字串,开始位置下标,结束位置下标)
**** 开始和结束可以省略,表示在整个字符串中查找
"""
"""
index() 检测某个字串是否包含在这个字符串中,
若【在】返回这个字符串开始的位置下标。
【不在】则报错
语法同上
"""
mystr = 'hello world and itcast and moon and python'
# 1.find()
# print(mystr.find('and')) # 12
# print(mystr.find('and', 15, 30)) # 23
# print(mystr.find('ands')) # -1 ands字符串不存在
# 2.index()
# print(mystr.index('and')) # 12
# print(mystr.index('and', 15, 30)) # 23
# print(mystr.index('ands')) # 若index查找的字符串不存在,则报错
# 3.count()
# print(mystr.count('and', 15, 30)) # 1
# print(mystr.count('and')) # 3
# print(mystr.count('ands')) # 0
"""
rfind() 和find()功能相同,但查找方向为右侧开始
rindex() 和index()功能相同,但查找方向为右侧开始
count() 返回某个字串在某个字符串中出现的次数
"""
# 4.rfind()
# print(mystr.rfind('and')) # 32
# print(mystr.find('moon')) # 27
# print(mystr.rfind('ands')) # -1
# 5. rindex()
# print(mystr.rindex('and')) # 32
# print(mystr.rindex('moon')) # 27
# print(mystr.rindex('ands')) # 报错
"""
find和rfind区别
find是正向搜查,
rfind是逆向搜查,
最终 两个函数返回的位置均是字符出现的正向位置
"""
2.修改
修改字符串,即通过函数的形式修改字符串中的数据
2.1 replace() 替换
"""
replace(); 替换
语法:
字符串序列.replace(旧字串, 新字串,替换次数)
"""
mystr = 'hi are you all right and have a bright day and you'
# replace 把 and 替换成 he
# new_mystr = mystr.replace('and', 'he') # 说明replace有返回值,返回值是修改后的字符串
# new_mystr = mystr.replace('and', 'he', 1) # 替换一次
new_mystr = mystr.replace('and', 'he', 10) # 10大于所需替换字符串在整个字符串中的次数,所以全部替换
print(mystr)
print(new_mystr)
# **** 调用replace函数后,发现原有字符串的数据并没有被修改,修改的数据是replace的返回值
# --- 说明字符串是不可变数据类型
# 数据 可以划分为 可变类型 和不可变类型
2.2 split() 分割
# 语法 字符串序列.split(分割字符, num)
mystr = '1 and 2 and 3 and 4'
# list1 = mystr.split('and')
# print(list1)
# list2 = mystr.split('and', 2) # 打印出的列表数是 2 + 1
# print(list2)
2.3 join() 合并
# join() --- 合并列表里的字符串数据为一个大字符串
# 语法 字符或字串.join(多字符串组成的序列)
mylist = ['aa', 'bb', 'cc']
# aa...bb...cc...
new_str = '...'.join(mylist)
print(new_str)
# 打印结果为aa...bb...cc...
2.4 大小写转换(非重点)
mystr = 'hello world and itcast and Python'
# 1.capitalize() 字符串首字母大写
# new_str =mystr. capitalize()
# 2. title() 字符串中每个首字母大写
# new_str =mystr.title()
# 3. upper() 字符串中 所有字母 小写转大写
# new_str =mystr.upper()
# 4. lower() 大写转小写
# new_str =mystr.lower()
print(new_str)
2.5 删除空白字符(非重点)
mystr = ' hello world and itcast and Python '
print(mystr)
# 1. lstrip() 删除字符串左侧空白字符
# new_str = mystr.lstrip()
# print(new_str)
# 2. rstrip() 删除字符串右侧空白字符
# new_str = mystr.rstrip()
# print(new_str)
# 3. strip() 删除字符串两侧空白字符
new_str = mystr.strip()
print(new_str)
2.6 字符串对齐(非重点)
# 1. ljust() 左对齐 返回一个原字符串左对齐,并使用指定字符(默认空格)填充至对应长度的 新字符
# 语法 字符串序列.ljust(长度,填充字符)
# 以下代码在Python Console中运行
# mystr = 'hello'
# mystr.ljust('10')
# mystr.ljust(10)
# mystr.ljust(10, '.') # 按 上箭头 ,可以重复上一个代码
# 结果 'hello.....'
# 2.rjust() 右对齐
# mystr.rjust(10)
# mystr.rjust(10, '.')
# 3. center() 居中对齐
# mystr.center(10)
# mystr.center(10, '.')
3.判断
判断,即判断真假,返回的结果是布尔型数据类型:True 或 False
mystr = 'hello world and itcast and Python'
# 1. startswith() 检查字符串是否以指定字符串开头。是,返回True;不是,返回False. 如果设置开始和结束位置下标,则在指定范围内查找。
# 语法 字符串序列.startswith(字串,开始位置下标,结束位置下标)
# print(mystr.startswith('hello')) # True
# print(mystr.startswith('hel')) # True
# print(mystr.startswith('hels')) # False
# 2. endswith() 判断字符串是否以某个字符串结尾
# print(mystr.endswith('Python')) # True
# print(mystr.endswith('Pythons')) # False
# 3. isalpha() 字符串都是字母,是 True
# print(mystr.isalpha())
# 4. isdigit() 都是数字,是 True
# print(mystr.isdigit())
mystr1 = '12345'
# print(mystr1.isdigit())
# 5. isalnum() 数字或字母或组合
# print(mystr1.isalnum())
# print(mystr.isalnum())
# mystr2 = 'abc123'
# print(mystr2.isalnum())
# 6. isspace() 空白
# print(mystr.isspace())
mystr3 = ' '
print(mystr3.isspace())