![cd603e9a3f94ac2b4964287c90d1f053.png](https://img-blog.csdnimg.cn/img_convert/cd603e9a3f94ac2b4964287c90d1f053.png)
字符串str
一、字符串定义
概念:字符串是有序的 不可修改的,元素以引号包围的序列
引号类型:''单引号 “”双引号 “ ””三引号 '''三单引号
print('python')
print("python")
print('''python''')
print("""python""")
![80647690817183f489e194adb5ff8958.png](https://img-blog.csdnimg.cn/img_convert/80647690817183f489e194adb5ff8958.png)
查看下打印出来的类型:
print(type('python'))
print(type("python"))
print(type('''python'''))
print(type("""python"""))
![254eaedfc22a8b2140735c98f83e2571.png](https://img-blog.csdnimg.cn/img_convert/254eaedfc22a8b2140735c98f83e2571.png)
单双引号适用范围
例如:打印字符串 what's it
print('what's it') 此句三个单引号,程序会报错,c此时改为
print("what's it")输出结果无误,单双引号便于区分字符串内容
![45139c742e453e675985a522938c0caf.png](https://img-blog.csdnimg.cn/img_convert/45139c742e453e675985a522938c0caf.png)
三引号和非三引号使用范围
三引号可用于多行注释,三单引号可用于字符串内换行
![a30b7764ea751b411304abc8d5538199.png](https://img-blog.csdnimg.cn/img_convert/a30b7764ea751b411304abc8d5538199.png)
二、字符串索引
例子:字符串:while
![e6947fbee632a958e5f078fea42be68c.png](https://img-blog.csdnimg.cn/img_convert/e6947fbee632a958e5f078fea42be68c.png)
索引:从0开始,while中每一个字母都是字符串的一个元素
索引常见的如下用法:
1.取单个元素, 字符串[索引值]——索引的元素
print('while'[2]) 运行得到结果,i
print('while'[2]) 运行得到结果,w
2.截取元素,字符串[start:end]——包括开始不包括结尾,默认取值从左至右
print('while'[0:3]) 运行得到结果,whi
3.步长截取,字符串[start:end:num],num-1截取
print('whileoejnd'[0:8:3]) 步长为3-1=2,运行结果 wle
print('whileoejnd'[0:8:2]) 步长为2-1=1,运行结果 wiee
4.默认截取,字符串[start:end:step],默认start从0开始,end到最后,step为1
print('while'[0:]) 运行结果while
print('while'[:3]) 运行结果whi
5.反取,字符串[负数],从右往左取值
![8dd2c1cb4228339203ecbd1405223563.png](https://img-blog.csdnimg.cn/img_convert/8dd2c1cb4228339203ecbd1405223563.png)
print('while'[-1]) 运行结果e
print('while'[::-1]) 运行结果elihw
三、字符串用法
(一)字符串的修饰
1.center函数:字符填充,并居中,若居中字符不平分,左小右大
print('while'.center(10)) while占5个字符,center定义10个,再填充5个,while居中,左2右三,自动填充空格
print('while'.center(10,*)) while占5个字符,center定义10个,再填充5个,while居中,左2右三,定义填充内容为*
![cc8bb77e15ddcd6ab1ed3b5794c97100.png](https://img-blog.csdnimg.cn/img_convert/cc8bb77e15ddcd6ab1ed3b5794c97100.png)
2.ljust函数,左对齐,右填充
![b82525ca23a589236592119ce9de5d2e.png](https://img-blog.csdnimg.cn/img_convert/b82525ca23a589236592119ce9de5d2e.png)
3.rjust,右对齐,左填充
![57c3397136e8a1710b9d7371202c843f.png](https://img-blog.csdnimg.cn/img_convert/57c3397136e8a1710b9d7371202c843f.png)
4.zfill,用字符0填充指定长度,从左到右填充
![ab938750bbca9400140db89f0a957fcd.png](https://img-blog.csdnimg.cn/img_convert/ab938750bbca9400140db89f0a957fcd.png)
5.format,通过位置填充字符串
![5c0c2407ec03de31bdae36ab0b8f88be.png](https://img-blog.csdnimg.cn/img_convert/5c0c2407ec03de31bdae36ab0b8f88be.png)
![59b9b0689b07dc98780c015811bf6323.png](https://img-blog.csdnimg.cn/img_convert/59b9b0689b07dc98780c015811bf6323.png)
6.strip,一处左右两边,默认为空格,strip内定义的字符,从左往右若开始读,若遇到非定义字符,则停止
![89af3eae5f0875b5ce3b2bc61496ca6e.png](https://img-blog.csdnimg.cn/img_convert/89af3eae5f0875b5ce3b2bc61496ca6e.png)
7. rstrip,移除左边
![9fbb0f9f338a878eba2d8ecfbae94673.png](https://img-blog.csdnimg.cn/img_convert/9fbb0f9f338a878eba2d8ecfbae94673.png)
8. lstrip,移除右边
![1557e449fc37b79ff37650a2bc30d8dc.png](https://img-blog.csdnimg.cn/img_convert/1557e449fc37b79ff37650a2bc30d8dc.png)
(二)字符串的查找
1.count,计数,返回指定字符在字符串中的个数
![b1ce4a4aeccd594f39e2485834842f62.png](https://img-blog.csdnimg.cn/img_convert/b1ce4a4aeccd594f39e2485834842f62.png)
2. find,查找,从左到右查询字符串中指定字符索引,遇到第一个指定字符就返回;找不到返回-1
![10a57827ca5bdab839868ae1c5896641.png](https://img-blog.csdnimg.cn/img_convert/10a57827ca5bdab839868ae1c5896641.png)
3. rfind,右查找,从右至左查询指定字符索引,遇到第一个字符返回;找不到返回-1
![72ea7e2f8d0404118b3515ef34f8eb1c.png](https://img-blog.csdnimg.cn/img_convert/72ea7e2f8d0404118b3515ef34f8eb1c.png)
4. index,索引,从左至右查找指定字符索引,找不到报错
![99260c74b67d9eec29f74bf56a174511.png](https://img-blog.csdnimg.cn/img_convert/99260c74b67d9eec29f74bf56a174511.png)
5. rindex,从右至左查到索引,找不到报错
![e7e3c281ba959f6e3d14e0a1fe4a2be7.png](https://img-blog.csdnimg.cn/img_convert/e7e3c281ba959f6e3d14e0a1fe4a2be7.png)
(三)字符串替换
1. replace,替换,从左至右替换指定元素,并可指定要替换个数
![b5220f7d6b1a52ccf717cf73a5a55b0d.png](https://img-blog.csdnimg.cn/img_convert/b5220f7d6b1a52ccf717cf73a5a55b0d.png)
2. translate,按照对应关系替换元素
![b077a73a500c70ddbf5feb3c013c4ec3.png](https://img-blog.csdnimg.cn/img_convert/b077a73a500c70ddbf5feb3c013c4ec3.png)
(四)字符串变形
1.upper,将字符串中全部小写字母转换成大写字母,数字其他符号不变
![1fa9bb3b875f6ff923b58888d8d40287.png](https://img-blog.csdnimg.cn/img_convert/1fa9bb3b875f6ff923b58888d8d40287.png)
2.lower,将字符串中全部大写字母转换成小写字母,数字其他符号不变
![3c72dbb1872be98bf9b67b9b804e834b.png](https://img-blog.csdnimg.cn/img_convert/3c72dbb1872be98bf9b67b9b804e834b.png)
3. swapcase ,将字符中全部字母大小写互换
![45afe60c3893bd58635110f399645635.png](https://img-blog.csdnimg.cn/img_convert/45afe60c3893bd58635110f399645635.png)
4. title,将字符串中每个单词首字母大写,(以空格识别)
![ad2e6cfbff521f449e66b2a0d5098ffb.png](https://img-blog.csdnimg.cn/img_convert/ad2e6cfbff521f449e66b2a0d5098ffb.png)
5. capitalize, 整个字符串的首字母大写
![af576a35da337b960e1103fbe938998b.png](https://img-blog.csdnimg.cn/img_convert/af576a35da337b960e1103fbe938998b.png)
(五)字符串判断
- isalnum, 判断字符串中是否全部由字母和数字组成,是返回TRUE 否返回FALSE
![d80b3a91e4368c6783fc6a975069ad1b.png](https://img-blog.csdnimg.cn/img_convert/d80b3a91e4368c6783fc6a975069ad1b.png)
2. isalpha ,判断字符串是否全部由字母组成
![2d3596f0e6eccbd2c1ad31fe03660814.png](https://img-blog.csdnimg.cn/img_convert/2d3596f0e6eccbd2c1ad31fe03660814.png)
3. isdigit, 判断是否全部由数字组成
4. isupper,判断是否全部由大写字母组成
5. islower,判断是否全部由小写字母组成
6. istitle ,判断字符串中每个单词的首字母是否大写
7. isspace ,判断字符串中是否完全由空格组成
8. startswith ,判断是否以指定字符开头,并可以设置截取判断
![7a8ac45063269ab5d79cd8fa0cc8a2f6.png](https://img-blog.csdnimg.cn/img_convert/7a8ac45063269ab5d79cd8fa0cc8a2f6.png)
9. endswith ,判断是否以指定字符结尾,设置截取判断
![d740b7418174fb62306cd7907cdf44e4.png](https://img-blog.csdnimg.cn/img_convert/d740b7418174fb62306cd7907cdf44e4.png)
(六)字符串的切分
1.splitlines ,按行切分
2. split,按空格从左至右切分,也可指定字符切分
3. rsplit , 从右至左切分
![141efaa94a8ffef389e384261e7b2a34.png](https://img-blog.csdnimg.cn/img_convert/141efaa94a8ffef389e384261e7b2a34.png)
(七)字符串的拼接
1.join,指定字符串,插入到2个元素之间,拼接成新的字符串
2. +,字符拼接
3.*,字符重复出现次数
![70b57de708bf2962c7d1f557eeefd189.png](https://img-blog.csdnimg.cn/img_convert/70b57de708bf2962c7d1f557eeefd189.png)