在Python中,字符串除了支持序列通用操作(包括双向索引、比较大小、计算长度、元素访问、切片、成员测试等)以外,还支持一些特有的用法,例如字符串格式化、查找、替换、排版等。但由于字符串属于不可变有序序列,不能直接对字符串对象进行元素增加、修改与删除等操作,切片操作也只能访问其中的元素而无法使用切片来修改字符串中的字符
常用方法
1、查找字符串find()
描述
find()
方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果指定范围内如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。
语法
find()
方法语法:
str.find(str, beg=0, end=len(string))
参数
- str – 指定检索的字符串
- beg – 开始索引,默认为0。
- end – 结束索引,默认为字符串的长度。
返回值
如果包含子字符串返回开始的索引值,否则返回-1。
实例
str1 = "Runoob example....wow!!!"
str2 = "exam"
print(str1.find(str2)) # 输出7
print(str1.find(str2, 5)) # 输出7
print(str1.find(str2, 1, 15)) # 输出7
print(str1.find(str2, 10)) # 输出-1
2、查找字符串rfind()
描述
Python rfind()
返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
语法
rfind()
方法语法:
str.rfind(str, beg=0 end=len(string))
参数
- str – 查找的字符串
- beg – 开始查找的位置,默认为0
- end – 结束查找位置,默认为字符串的长度。
返回值
返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
实例
str1 = "this is really a string example....wow!!!"
str2 = "is"
print(str1.rfind(str2)) # 输出5
print(str1.rfind(str2, 10)) # 输出-1
print(str1.rfind(str2, 0, 10)) # 输出5
print(str1.rfind(str2, 10, 0)) # 输出-1
3、查找字符串index()
描述
index()
方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()
方法一样,只不过如果str不在 string中会报一个异常。
语法
index()方法语法:
str.index(str, beg=0, end=len(string))
参数
- str – 指定检索的字符串
- beg – 开始索引,默认为0。
- end – 结束索引,默认为字符串的长度。
返回值
如果包含子字符串返回开始的索引值,否则抛出异常。
实例
str1 = "Runoob example....wow!!!"
str2 = "exam"
print(str1.index(str2)) # 输出7
print(str1.index(str2, 5)) # 输出7
print(str1.index(str2, 1, 15)) # 输出7
print(str1.index(str2, 10)) # 元素找不到,抛出ValueError: substring not found异常
4、查找字符串rindex()
描述
rindex()
返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间。
语法
rindex()
方法语法:
str.rindex(str, beg=0 end=len(string))
参数
- str – 查找的字符串
- beg – 开始查找的位置,默认为0
- end – 结束查找位置,默认为字符串的长度。
返回值
返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常。
实例
str1 = "this is really a string example....wow!!!"
str2 = "is"
print(str1.rindex(str2)) # 输出5
print(str1.rindex(str2, 0, 10)) # 输出5
print(str1.rindex(str2, 10, 0)) # 元素找不到,抛出ValueError: substring not found异常
5、统计字符串出现的次数count()
描述
count()
方法用于统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置。
语法
count()
方法语法:
str.count(sub, start= 0,end=len(string))
参数
- sub – 搜索的子字符串
- start – 字符串开始搜索的位置。默认为第一个字符,第一个字符索引值为0。
- end – 字符串中结束搜索的位置。字符中第一个字符的索引为 0。默认为字符串的最后一个位置。
返回值
该方法返回子字符串在字符串中出现的次数,不存在时返回0。
实例
str = "www.runoob.com"
sub = 'o'
# 输出str.count('o') : 3
print("str.count('o') : ", str.count(sub))
sub = 'run'
# 输出str.count('run', 0, 10) : 1
print("str.count('run', 0, 10) : ", str.count(sub, 0, 10))
6、分割字符串split()
描述
split()
通过指定分隔符对字符串进行切片,如果第二个参数 num 有指定值,则分割为 num+1 个子字符串。
语法
split()
方法语法:
str.split(str="", num=string.count(str))
参数
- str – 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
- num – 分割次数。默认为 -1, 即分隔所有。
返回值
返回分割后的字符串列表。
实例
str = "this is string example....wow!!!"
# 以空格为分隔符
print(str.split()) # 输出['this', 'is', 'string', 'example....wow!!!']
# 以 i 为分隔符,分割1次
print(str.split('i', 1)) # 输出['th', 's is string example....wow!!!']
# 以 i 为分隔符,分割3次
print(str.split('i', 3)) # 输出['th', 's ', 's str', 'ng example....wow!!!']
# 以 w 为分隔符
print(str.split('w')) # 输出['this is string example....', 'o', '!!!']
7、分割字符串partition()
描述
partition()
方法用来根据指定的分隔符从左往右将字符串进行分割。
如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
语法
partition()
方法语法:
str.partition(str)
参数
str : 指定的分隔符。
返回值
返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
实例
str = "www.runoob.com"
print(str.partition(".")) # 输出('www', '.', 'runoob.com')
8、分割字符串rpartition()
描述
rpartition()
方法类似于 partition()
方法,只是该方法是从目标字符串的末尾也就是右边开始搜索分割符。。
如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
语法
rpartition()
方法语法:
str.rpartition(str)
参数
str : 指定的分隔符。
返回值
返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
实例
str = "www.runoob.com"
print(str.rpartition(".")) # 输出('www.runoob', '.', 'com')
9、连接字符串join()
描述
join()
方法用于将序列中的元素以指定的字符连接生成一个新的字符串。
语法
join()
方法语法:
str.join(sequence)
参数
- str :指定的连接符
- sequence :要连接的元素序列。
返回值
返回通过指定字符连接序列中元素后生成的新字符串。
实例
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print(s1.join(seq)) # 输出r-u-n-o-o-b
print(s2.join(seq)) # 输出runoob
list1 = ['1', '2', '3']
print(type(list1)) # 输出<class 'list'>
# 将列表变为字符串
s3 = ','.join(list1)
print(s3, type(s3)) # 输出1,2,3 <class 'str'>
10、字符串大小写转换
描述
lower()
方法转换字符串中所有大写字符为小写。
upper()
方法将字符串中的小写字母转为大写字母。
capitalize()
将字符串的第一个字母变成大写,其他字母变小写
title()
方法返回”标题化”的字符串,就是说所有单词的首个字母转化为大写,其余字母均为小写
swapcase()
方法用于对字符串的大小写字母进行转换
语法
都与lower()
方法语法相同:
str.lower()
参数
都没有参数
返回值
返回对应的大小写转换后新生成的字符串
实例
s = "What is Your Name?"
print(s.lower()) # 输出what is your name?
print(s.upper()) # 输出WHAT IS YOUR NAME?
print(s.capitalize()) # 输出What is your name?
print(s.title()) # 输出What Is Your Name?
print(s.swapcase()) # 输出wHAT IS yOUR nAME?
11、替换字符串replace()
描述
replace()
方法把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
语法
replace()
方法语法:
str.replace(old, new[, max])
参数
- old – 将被替换的子字符串。
- new – 新字符串,用于替换old子字符串。
- max – 可选字符串, 替换不超过 max 次
返回值
返回字符串中的 old(旧字符串) 替换成 new(新字符串)后生成的新字符串,如果指定第三个参数max,则替换不超过 max 次。
实例
str = "www.w3cschool.cc"
# 输出菜鸟教程旧地址: www.w3cschool.cc
print("菜鸟教程旧地址:", str)
# 输出菜鸟教程新地址: www.runoob.com
print("菜鸟教程新地址:", str.replace("w3cschool.cc", "runoob.com"))
str = "this is string example....wow!!!"
# 输出thwas is string example....wow!!!
print(str.replace("is", "was", 1)) # 指定替换一次
12、删除空格或指定字符strip()
描述
strip()
方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
语法
strip()
方法语法:
str.strip([chars]);
参数
- chars – 移除字符串头尾指定的字符序列。
返回值
返回移除字符串头尾指定的字符序列生成的新字符串。
实例
str = "*****this is **string** example....wow!!!*****"
# 输出this is **string** example....wow!!!
print(str.strip('*')) # 指定字符串 *
# 输出3abcrunoob3
str = "123abcrunoob321"
print (str.strip( '12' )) # 字符序列为 12
13、删除空格或指定字符rstrip()
描述
rstrip()
删除 string 字符串末尾的指定字符(默认为空格)
语法
rstrip()
方法语法:
str.rstrip([chars])
参数
- chars – 指定删除的字符(默认为空格)
返回值
返回删除 string 字符串末尾的指定字符后生成的新字符串。
实例
str = " this is string example....wow!!! "
print (str.rstrip()) # 输出 this is string example....wow!!!!
str = "*****this is string example....wow!!!*****"
print (str.rstrip('*')) # 输出*****this is string example....wow!!!
14、删除空格或指定字符lstrip()
描述
lstrip()
方法用于截掉字符串左边的空格或指定字符。
语法
lstrip()
方法语法:
str.lstrip([chars])
参数
- chars –指定截取的字符。
返回值
返回截掉字符串左边的空格或指定字符后生成的新字符串。
实例
str = " this is string example....wow!!! "
print(str.lstrip()) # 输出this is string example....wow!!!
str = "88888888this is string example....wow!!!8888888"
print(str.lstrip('8')) # 输出this is string example....wow!!!8888888
15、适用于字符串对象的内置函数
x = 'Hello world.'
# 字符串长度
print(len(x)) # 输出12
# 最大字符 # 输出w
print(max(x))
# 最小字符
print(min(x)) # 输出''
# eval()用来把任意字符串转化为python表达式并进行求值
print(eval("3+4")) # 输出7