1.find()和rfind方法分别用来查找一个字符串在另一个字符串指定范围(默认是整个字符串)中首次和最后一次出现的位置,如果不存在则返回-1;
例1,
s='apple,peach,banana,peach,pear'
print(s.find('peach')) ##返回第一次出现的位置
##6
print(s.find("peach",7)) ##从指定位置开始查找
##19
print(s.find("peach",7,20)) # 在指定范围查询
###-1 不存在
print(s.rfind('p')) #从尾部向前查询
##25
2.index()和rindex()方法用来返回一个字符串在另一个字符串指定范围中首次和最后一次出现的位置,如果不存在则抛出异常;
# count()方法用来返回一个字符串在另一个字符串中出现的次数。
s='apple,peach,banana,peach,pear'
print(s.count('p'))
##5
print(s.index('a'))
##0
print(s.index('aaa'))
#抛出异常
Traceback (most recent call last):
File "C:/Users/86130/Desktop/PYCHARM/随意.py", line 142, in <module>
print(s.index('aaa'))
ValueError: substring not found
3.字符串--字母大小变换
# lower()返回小写字符串
# upper()返回大写字符串
# capitalize()首字符大写
# title()每个单词的首字母大写
# swapcase()大小写互换
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?
4.字符串分割
# split()和rsplit()方法分别用来以指定字符为分隔符,将字符串左端和右端开始将其分割成多个字符串,并返回包含分割结果的列表。
#格式:str.split(sep,maxsplit)、str.rsplit(sep,maxsplit)
#sep:用于指定分隔符,可以包含多个字符。此参数默认为 None,表示所有空字符,包括空格、换行符“\n”、制表符“\t”等。
#maxsplit:可选参数,用于指定分割的次数,最后列表中子串的个数最多为 maxsplit+1。如果不指定或者指定为 -1,则表示分割次数没有限制。
s= 'hello world \n\n My name is Dong'
print(s.split()) #不指定分隔符,则字符串中的任何空白符号(包括空格、换行符、制表符等等)都将被认为是分隔符,返回包含最终分割结果的列表。
##['hello', 'world', 'My', 'name', 'is', 'Dong']
print(s.split(' ')) #指定分隔符为空格
##['hello', 'world', '\n\n', 'My', 'name', 'is', 'Dong']
print(s.split(' ',2)) #指定分隔符为空格并且切割2次
##['hello', 'world', '\n\n My name is Dong']
5.jion方法
a='Iloveyou'
b='/'
s=b.join(a)
print(s)
##I/l/o/v/e/y/o/u
a=["I","love","you"]
sep="/"
s=sep.join(a)
print(s)
##I/love/you
6.partition()和rpartition()用来以指定字符串为分隔符将原字符串分割为3部分,即分隔符前的字符串、分隔符字符串、分隔符后的字符串,如果指定的分隔符不在原字符串中,则返回原字符串和两个空字符串。
s="apple,peach,banana,pear"
print(s.partition(',')) # #从左侧使用逗号进行切分
#('apple', ',', 'peach,banana,pear')
print(s.rpartition(',')) #从右侧使用逗号进行切分
#('apple,peach,banana', ',', 'pear')
print(s.rpartition('banana')) #使用字符串作为分隔符
#('apple,peach,', 'banana', ',pear')
7.字符串替换操作replace()
基本用法:str.replace(old, new)
s = "Python is a greate programming language. I like it!"
a=s.replace('a','u')
print(a)
8.字符串--映射
#字符串对象的maketrans()方法用来生成字符映射表,而translate()方法用来根据映射表中定义的对应关系转换字符串并替换其中的字符,使用这两个方法的组合可以同时处理多个不同的字符,replace()方法则无法满足这一要求。
s = "Python is a greate programming language. I like it!"
table = ''.maketrans('abcdef123', 'uvwxyz@#$') ##前面是单引号,''.maketrans()
x=s.translate(table)
print(x)
##Python is u gryuty progrumming lunguugy. I liky it!
9.字符串删除操作(剥洋葱)
strip(): 两端删除操作
rstrip() :右端删除操作
lstrip():左端删除操作
10.eval()
#内置函数eval():尝试将任意字符串转化为表达式并求值
a=3
b=5
print('a+b')
##a+b
print(eval('a+b'))
##8
11.字符串对齐操作
#center()、ljust()、rjust(),返回指定宽度的新字符串,原字符串居中、左对齐或右对齐出现在新字符串中,如果指定宽度大于字符串长度,则使用指定的字符(默认为空格)进行填充。
s='Hello world!'
print(s.center(20))#居中对齐,宽度不够以空格进行填充
# Hello world!
print(s.center(20,'='))#居中对齐,以字符=进行填充
#====Hello world!====
12.起始或终止符判定
#s.startswith(t)、s.endswith(t),判断字符串是否以指定字符串开始或结束
s = 'Beautiful is better than ugly.'
print(s.startswith('Be')) #检测整个字符串
#True
print(s.startswith('Be', 5)) #指定检测范围起始位置
#False
print(s.startswith('Be', 0, 5)) # 指定检测范围起始和结束位置
#True
13.zfill()返回指定宽度的字符串,在左侧以字符0进行填充。指定宽度小于字符串长度时,返回字符串本身
print('abc'.zfill(5))
##00abc
print('abc'.zfill(1))
##abc
14.字符串元素类型判定
isalnum()、isalpha()、isdigit()、isdecimal()、isnumeric()、isspace()、isupper()、islower(),用来测试字符串是否为数字或字母、是否为字母、是否为数字字符、是否为空白字符、是否为大写字母以及是否为小写字母。
15.Python标准库zlib中提供的compress()和decompress()函数可以用于数据的压缩和解压缩,在压缩字符串之前需要先编码为字节串。
import zlib
x=('Python程序设计系列图书'*8).encode()
print(len(x))
#240
y=zlib.compress(x)
print(len(y))
#44
z=zlib.decompress(y)
print(len(z))
#240
16.字符串常量,Python标准库string中定义数字字符、标点符号、英文字母、大写字母、小写字母等常量。
import string
print(string.digits)
print(string.punctuation)
print(string.ascii_letters)
print(string.ascii_lowercase)
print(string.ascii_uppercase)
#结果:
0123456789
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ