字符串的常用操作
字符串的查询操作
- index()查找子串substr第一次出现的位置,如果查找的子串不存在是,则抛出ValueError
str=('python,python')
print(str.index('on'))#on第一次出现的位置
#4
- rindex()查找子串substr最后一次出现的位置,如果查找的子串不存在时,则抛出ValueError
str=('python,python')
print(str.rindex('on'))#on最后一次出现的位置
#11
- find()查找子串substr第一次出现的位置,如果查找的子串不存在时,则返回-1
str=('python,python')
print(str.rfind('on'))#on第一次出现的位置
#4
- rfind()查找子串substr最后一次出现的位置,如果查找的子串不存在时,则返回-1
str=('python,python')
print(str.rfind('on'))#on最后一次出现的位置
#11
字符串的大小写转换操作的方法
upper() 把字符串中所有字符都转换成大写字母
s='python'
a=s.upper()
print(a)
#PYTHON
lower() 把字符串中所有字符都转成小写字母
s='PYthon'
a=s.lower()
print(a)
#python
swapcase() 把字符串中所有大写字母转成小写字母,把所有小写字母都转成大写字母
s='PYthon'
print(s.swapcase())
#pyTHON
capitalize() 把第一个字符转换为大写,把其余字符转换为小写
s='PYthon'
print(s.capitalize())
#Python
title() 把每个单词的第一个字符转换为大写,把每个单词剩余字符串转换为小写
s='PYthon hello'
print(s.title())
#Python Hel
字符串内容对齐操作的方法
center() 居中对齐,第一个参数指定宽度,第二个参数指定填充符,第二个参数是可选的,默认是空格,如果设置宽度小于实际宽度则返回原字符串
s='PYthon hello'
print(s.center(20,'*'))
#****PYthon hello****
ljust()左对齐
s='PYthon hello'
print(s.ljust(20,'*'))
#PYthon hello********
rjust()右对齐
s='PYthon hello'
print(s.rjust(20,'*'))
#********PYthon hello
zfill()右对齐,左边用0填充,该方法只接收一个参数,用于指定字符串的宽度
s='PYthon hello'
print(s.zfill(20))
#00000000PYthon hello
字符串劈分操作的方法
- split() 从字符串的左边开始劈分,默认的劈分字符是空格,返回的值都是一个列表
s='PYthon hello hello'
a=s.split()
print(a)
#['PYthon', 'hello', 'hello']
通过参数sep指定劈分字符串
s='PYthon**hello**hello'
a=s.split(sep='**')
print(a)
#['PYthon', 'hello', 'hello']
通过参数maxsplit指定劈分字符串最大劈分次数
s='PYthon**hello**hello'
a=s.split(sep='**',maxsplit=1)
print(a)
#['PYthon', 'hello**hello']
- rsplit() 从字符串的右边开始劈分,默认的劈分字符是空格字符,返回的值都是一个列表
s='PYthon**hello**hello'
a=s.rsplit(sep='**',maxsplit=1)
print(a)
#['PYthon**hello', 'hello']
判断字符串操作的方法
isidentifier() 判断指定的字符串是不是合法的标识符
print('abc_12'.isidentifier())#True
print('aba'.isidentifier())#True
print('你好'.isidentifier())#True
print('@abc'.isidentifier())#False
print('12345'.isidentifier())#False
isspace() 判断指定的字符串是否全部由空白字符组成(回车,换行,水平制表符)
print('\t'.isspace())#True
print(' '.isspace())#True
print('abc'.isspace())#False
isalpha() 判断指定的字符串是否全部由字母组成
print('adc123'.isalpha())#False
print('你好'.isalpha())#True
print('@!'.isalpha())#False
isdecimal() 判断指定字符串是否全部由十进制的数字组成
print('1234'.isdecimal())#True
print('123四'.isdecimal())#False
print('壹贰叁肆'.isdecimal())#False
isnumeric() 判断指定的字符串是否全部由数字组成
print('1234'.isnumeric())#True
print('123四'.isnumeric())#True
print('壹贰叁肆'.isnumeric())#True
print('阿凡达'.isnumeric())#False
isalnum() 判断指定字符串是否全部由字符和数字组成
print('abd'.isalnum())#True
print('123'.isalnum())#True
print('abd123'.isalnum())#True
字符串操作的其他方法
1.字符串的替换
replace() 第一个参数指定被替换的子串,第2个参数指定替换子串的字符串,该方法返回替换后得到的字符串,替换前的字符串不会发生变化,调用该方法时可以通过第3个参数指定最大替换次数
str='hello,python,python'
print(str.replace('python','hello'))
#hello,hello,hello
print(str.replace('python','你好',1))#替换一次
#hello,你好,python
2.字符串的合并
join() 将列表或元组中的字符串合并成一个字符串
str=['hello','python','python']
print('|'.join(str))
#hello|python|python
3.字符串的切片操作
字符串是不可变类型,不具备增删改等操作,切片操作将产生新的对象
str='hello,python'
print(str[1:5:1])
#ello
4.格式化字符串
name='小明'
age=18
print('我叫%s,今年%d岁' % (name,age))
print('我叫{0},今年{1}岁'.format(name,age))
print(f'我叫{name},今年{age}岁')
'''
我叫小明,今年18岁
我叫小明,今年18岁
我叫小明,今年18岁
'''
5.字符串的编码转换
s='床前明月光'
#编码
s1=s.encode(encoding='GBK')
print(s1)
#'\xb4\xb2\xc7\xb0\xc3\xf7\xd4\xc2\xb9\xe2'
#解码
print(s1.decode(encoding='GBK'))
#床前明月光