常用的操作方法:查找/修改/判断
一、常用的操作方法
字符串的常⽤操作⽅法有查找、修改和判断三⼤类。
1、查找
所谓字符串查找⽅法即是查找⼦串在字符串中的位置或出现的次数。
find():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则返
回-1。
语法:
字符串序列.find(⼦串, 开始位置下标, 结束位置下标)
#注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
举例:
mystr = "hello world and itcast and itheima and Python"
print(mystr.find('and')) # 12
print(mystr.find('and', 15, 30)) # 23
print(mystr.find('ands')) # -1
index():检测某个⼦串是否包含在这个字符串中,如果在返回这个⼦串开始的位置下标,否则则
报异常。
语法:
字符串序列.index(⼦串, 开始位置下标, 结束位置下标)
#注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
举例:
mystr = "hello world and itcast and itheima and Python"
print(mystr.index('and')) # 12
print(mystr.index('and', 15, 30)) # 23
print(mystr.index('ands')) # 报错
拓展:
rfind(): 和find()功能相同,但查找⽅向为右侧开始。
rindex():和index()功能相同,但查找⽅向为右侧开始。
count():返回某个⼦串在字符串中出现的次数
语句:
字符串序列.count(⼦串, 开始位置下标, 结束位置下标)
#注意:开始和结束位置下标可以省略,表示在整个字符串序列中查找。
举例
mystr = "hello world and itcast and itheima and Python"
print(mystr.count('and')) # 3
print(mystr.count('ands')) # 0
print(mystr.count('and', 0, 20)) # 1
2、修改
所谓修改字符串,指的就是通过函数的形式修改字符串中的数据。
replace():替换
语句:
字符串序列.replace(旧⼦串, 新⼦串, 替换次数)
#注意:替换次数如果查出⼦串出现次数,则替换次数为该⼦串出现次数。
举例
mystr = "hello world and itcast and itheima and Python"
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he'))
# 结果:hello world he itcast he itheima he Python
print(mystr.replace('and', 'he', 10))
# 结果:hello world and itcast and itheima and Python
print(mystr)
注意:数据按照是否能直接修改分为可变类型和不可变类型两种。字符串类型的数据修改的时候
不能改变原有字符串,属于不能直接修改数据的类型即是不可变类型。
重点:split():按照指定字符分割字符串。
语法:
字符串序列.split(分割字符, num)
#注意:num表示的是分割字符出现的次数,即将来返回数据个数为num+1个。
举例:
mystr = "hello world and itcast and itheima and Python"
# 结果:['hello world ', ' itcast ', ' itheima ', ' Python']
print(mystr.split('and'))
# 结果:['hello world ', ' itcast ', ' itheima and Python']
print(mystr.split('and', 2))
# 结果:['hello', 'world', 'and', 'itcast', 'and', 'itheima', 'and', 'Python']
print(mystr.split(' '))
# 结果:['hello', 'world', 'and itcast and itheima and Python']
print(mystr.split(' ', 2))
注意:如果分割字符是原有字符串中的⼦串,分割后则丢失该⼦串。
3、判断
所谓判断即是判断真假,返回的结果是布尔型数据类型:True 或 False。
startswith():检查字符串是否是以指定⼦串开头,是则返回 True,否则返回 False。如果设置开
始和结束位置下标,则在指定范围内检查。
语法:
字符串序列.startswith(⼦串, 开始位置下标, 结束位置下标)
** 举例:**
mystr = "hello world and itcast and itheima and Python "
# 结果:True
print(mystr.startswith('hello'))
# 结果False
print(mystr.startswith('hello', 5, 20))
endswith()::检查字符串是否是以指定⼦串结尾,是则返回 True,否则返回 False。如果设置开
始和结束位置下标,则在指定范围内检查。
语法:
字符串序列.endswith(⼦串, 开始位置下标, 结束位置下标)
举例:
mystr = "hello world and itcast and itheima and Python"
# 结果:True
print(mystr.endswith('Python'))
# 结果:False
print(mystr.endswith('python'))
# 结果:False
print(mystr.endswith('Python', 2, 20))
isalpha():如果字符串⾄少有⼀个字符并且所有字符都是字⺟则返回 True, 否则返回 False。
mystr1 = 'hello'
mystr2 = 'hello12345'
# 结果:True
print(mystr1.isalpha())
# 结果:False
print(mystr2.isalpha())
isdigit():如果字符串只包含数字则返回 True 否则返回 False。
mystr1 = 'aaa12345'
mystr2 = '12345'
# 结果: False
print(mystr1.isdigit())
# 结果:False
print(mystr2.isdigit())
isalnum():如果字符串⾄少有⼀个字符并且所有字符都是字⺟或数字则返 回 True,否则返回
False。
mystr1 = 'aaa12345'
mystr2 = '12345-'
# 结果:True
print(mystr1.isalnum())
# 结果:False
print(mystr2.isalnum())
isspace():如果字符串中只包含空⽩,则返回 True,否则返回 False。
mystr1 = '1 2 3 4 5'
mystr2 = ' '
# 结果:False
print(mystr1.isspace())
# 结果:True
print(mystr2.isspace())
总结:
下标:
计算机为数据序列中每个元素分配的从0开始的编号
切⽚:
序列名[开始位置下标:结束位置下标:步⻓]
常⽤操作⽅法:
find()
index()

浙公网安备 33010602011771号