python为字符串操作提供了很多常用的内建函数,让我们在操作字符串时能够更加简单方便。
下面以某个字符串为例来一一介绍一下
现有字符串:mystr = ‘hello world and hello python’
find(self, sub, start=None,end=None)
find 函数用于在给定的字符串中查找某个子字符串是否存在,如果找到则返回该子串的第一次出现的索引位置,否则返回-1
其中,self参数不用传递,sub就是要找的子字符串,start和end分别是字符串的开始和结束位置,如果不传递则默认从字符串的开始到字符串的结束位置。
使用方式:字符串.find(),比如查找python出现的位置
mystr = 'hello world and hello python'
#1.在整个字符串中查找
print(mystr.find('python'))
#输出结果
22
#2.在字符串的指定位置内查找
print(mystr.find('python',0,20))
#返回结果
-1
rfind(self, sub, start=None,end=None)
与find用法类似,只不过是从字符串的右侧开始查找,但索引依然是从左边计算
mystr = 'hello world and hello python'
#1.在整个字符串中查找
print(mystr.rfind('python'))
#输出结果
22
index(self,sub,start=None,end=None)
index函数与find函数的参数和用法一样,也是直接返回子串的索引位置。唯一不同的是当没有找到子串时不会返回-1而是直接报异常。示例代码:
mystr = 'hello world and hello python'
#1.在整个字符串中查找
print(mystr.index('python'))
#输出结果
22
#2.在字符串的指定位置内查找
print(mystr.index('python',0,20))
#返回结果
报异常:ValueError:substring not found
rindex(self,sub,start=None,end=None)
与index函数用法和功能一下,不同的是也是从字符串的右侧开始查找,但索引依然是从左侧计算
mystr = 'hello world and hello python'
#1.在整个字符串中查找
print(mystr.rindex('python'))
#输出结果
22
count(self,sub,start=None,end=None)
count函数用于统计sub子串在字符串指定位置中出现的次数,start和end不传递则默认匹配整个字符串
#1.在整个字符串中统计
print(mystr.count('python'))
#输出结果
1
#2.在字符串的指定位置内统计
print(mystr.count('python',0,20))
#返回结果
0
replace(self,oldstr,newstr,count)
replace用法还是与其它函数用法相同,但参数有所不同。oldstr将被替换的字符串,newstr替换后新的字符串,count为int类型如果不传递则默认替换所有的oldstr,如果count指定了值则替换不超过count次。返回值为替换后的新字符串
#1.替换所有匹配到的字符串
print(mystr.replace('hello','hi'))
#输出结果
hi world and hi python
#2.只替换一次
print(mystr.replace('hello','hi',1))
#返回结果
hi world and hello python
split(sub,maxsplit)
split函数用法不变,主要用于以sub为分隔符对整个字符串切片,可以不传递任何参数,则默认会根据空格进行切片,如果maxsplit有指定值,则仅分割maxsplit个子字符串。其返回结果为列表类型
#1.替换所有匹配到的字符串
print(mystr.split())
print(mystr.split(' '))
#两条语句返回结果相同
['hello','world','and','hello','python']
['hello','world','and','hello','python']
#2.只分割2个
print(mystr.split(' ',2))
#返回结果
['hello','world','and hello python']
capitalize
capitalize用于将字符串的第一个字符转换为大写,返回值为转换后的新字符串
print(mystr.capitalize())
#输出结果
Hello world and hello python
title
title函数与cacapicapitalize函数类似也是把字符串转换为大写,但不同的是title函数会将字符串中的每个单词的首字母都转换为大写,返回值为转换后的新字符串
print(mystr.title())
#输出结果
Hello World And Hello Python
startswith
startswith函数用于判断一个字符串是否是以某个子字符串开头,如果是返回True否则返回False
#1. 判断是否以hello开头
print(mystr.startswith('hello'))
#输出结果
True
#2. 判断是否以python开头
print(mystr.startswith('python'))
#输出结果
Flase
endwith
与startswith相同用于判断是否以某个字符串结尾,是返回True,否则返回False
#1. 判断是否以python结尾
print(mystr.endwith('python'))
#输出结果
True
#2. 判断是否以hello结尾
print(mystr.endwith('hello'))
#输出结果
Flase
lower
lower函数用于将字符串中所有大写字符转换为小写
mystr = 'HELLO WORLD and Hello Python'
print(mystr.lower())
#输出结果
hello world and hello python
upper
upper函数与lower函数相反,是将字符串中所有小写字母全部转换为大写
mystr = 'hello world and Hello Python'
print(mystr.upper())
#输出结果
HELLO WORLD AND HELLO PYTHON
ljust(width) rjust(width) center(width)
这是3个调整字符串对齐的函数,参数width表示字符串的长度,如果原始字符串长度不够则用空格来填充,最终返回一个新的字符串
mystr = 'hello'
print(mystr.ljust(10))
print(mystr.rjust(10))
print(mystr.center(10))
#输出结果
'hello '
' hello'
' hello '
lstrip()
用于删除字符串左侧的所有空格,返回操作后的新字符串
mystr = ' hello python'
print(mystr.lstrip())
#输出结果
hello python
rstrip()
用于删除字符串右侧的所有空格,返回操作后的新字符串
mystr = 'hello python '
print(mystr.rstrip())
#输出结果
hello python
strip()
删除字符串左右两侧的所有空格
mystr = ' hello python '
print(mystr.strip())
#输出结果
hello python
partition()
该函数用于将一个字符串以某个子串分割为3个部分,分别是子串前,子串和子串后,返回一个分割后的列表
mystr = 'hello wrold and hello python'
mystr.partition('and')
#输出结果
['hello world','and','hello python']
rpartition()
与partition一样也是将一个字符串以某个子串分割为3个部分,分别是子串前,子串和子串后,返回一个分割后的列表。不同的是该函数将从字符串的右侧开始分割
mystr = 'hello python and python hello'
mystr.rpartition('python')
#输出结果
['hello python and','python','hello']
splitlines()
splitlines函数用于以行为单位对字符串进行分割,结果返回分割后的列表n
等同于split('\n')
mystr = 'hello\nworld\nand\nhello\npython'
print(mystr.splitlines())
print(mystr.split('\n'))
#输出结果
['hello','world','and','hello','python']
['hello','world','and','hello','python']
isalpha()
isalpha用于判断一个字符串中,是否所有的字符都是字母,如果是返回True否则返回False
mystr = 'hello world'
print(mystr.isalpha())
#输出结果
True
mystr = 'hello 123'
print(mystr.isalpha())
#输出结果
False
mystr = '123'
print(mystr.isalpha())
#输出结果
False
isdigit()
isdigit用于判断一个字符串中,是否所有的字符都是数字,如果是返回True否则返回False
mystr = 'hello world'
print(mystr.isdigit())
#输出结果
False
mystr = 'hello 123'
print(mystr.isdigit())
#输出结果
False
mystr = '123'
print(mystr.isdigit())
#输出结果
True
isalnum()
isalnum用于判断一个字符串中,是否所有的字符只包含字母或者数字,如果是返回True否则返回False
mystr = 'helloworld'
print(mystr.isalnum())
#输出结果
True
mystr = 'hello world'
print(mystr.isalnum())
#输出结果
False
mystr = 'hello 123'
print(mystr.isalnum())
#输出结果
False
mystr = 'hello123'
print(mystr.isalnum())
#输出结果
True
mystr = '123'
print(mystr.isalnum())
#输出结果
True
isspace()
isspace用于判断一个字符串中,是否只包含空格,如果是返回True否则返回False
mystr = 'helloworld'
print(mystr.isspace())
#输出结果
False
mystr = 'hello world'
print(mystr.isspace())
#输出结果
False
mystr = ''
print(mystr.isspace())
#输出结果
False
mystr = ' '
print(mystr.isspace())
#输出结果
True
mystr = ' '
print(mystr.isspace())
#输出结果
True
join()
join的参数一般是一个可迭代对象,如列表、元组或集合,用于将可迭代对象中的元素以某个字符串进行连接,组合成一个新的字符串,
参数也可以是一个字符串,那么组合后的字符串就是在每个字符后面插入某个连接字符
#1.参数是一个列表
lst = ['hello','world','and','hello','python']
#以空格的形式进行连接
print(' '.join(lst))
#以下划线的形式进行连接
print('_'.join(lst))
#输出结果
hello_world_and_hello_python
#2. 参数是一个字符串
mystr = 'hello world and hello python'
print('_'.join(mystr))
#输出结果
h_e_l_l_o_ _w_o_r_l_d_ _a_n_d_ _h_e_l_l_o_ _p_y_t_h_o_n