1、字符串的定义
单引号
双引号
三引号
字符串定义时,引号可以相互嵌套
2、转义字符
斜杠\转义符,续行符.转义符可转义,在末尾可续行
strs='hello'\'wolrd'print(strs)#hellowolrd
\n 换行
\r 代表光标位置(从\r的位置作为光标的起点)
\t 代表水平制表符
\b 代表退格符(删除前一个字符)
\ 代表斜杠本身,避免转义
不想字符串有转义字符,在字符串前加r
strs=r'hello \b wolrd'print(strs)#hello \b wolrd
3、字符串操作
+操作:字符串拼接*操作:字符串重复strs='hello'res=strs*2print(res)#hellohello
[ ]切片操作:字符串索引操作,只能访问不能修改,索引可以是负数
str[开始值,结束值,步进值]
strs='helloworld!'print(strs[1])#eres1=strs[1:9:2]print(res1)#elwrres2=strs[1:9]print(res2)#elloworlres3=strs[::2]print(res3)#hlool!
4、字符串格式化format()
普通方法
name='李白'strs='{}成舟将欲行,忽闻岸上{}声'.format(name,'踏歌')print(strs)#李白成舟将欲行,忽闻岸上踏歌声
通过索引传参方法
大括号内是索引值
name='李白'strs='{2}成舟将欲行,忽闻岸上{1}声'.format('temp','踏歌',name)print(strs)#李白成舟将欲行,忽闻岸上踏歌声
通过关键字传参方法
strs='{name}成舟将欲行,忽闻岸上{action}声'.format(name='李白',action='踏歌')print(strs)#李白成舟将欲行,忽闻岸上踏歌声
通过容器传参方法
使用对象传参的话,需要使用两个星号**data
strs = '豪放派:{0[0]},婉约派:{0[1]},蛋黄派:{0[2]}'.format(['李白', '辛弃疾', '达利园'])print(strs) # 豪放派:李白,婉约派:辛弃疾,蛋黄派:达利园strs2 = '豪放派:{0[0]},婉约派:{1[1]},蛋黄派:{1[2]}'.format(['李白', '辛弃疾', '达利园'],['陆游','杜甫','好丽友'])print(strs2) # 豪放派:李白,婉约派:辛弃疾,蛋黄派:达利园data={'a':'李白','b':'踏歌'}strs3='{a}成舟将欲行,忽闻岸上{b}声'.format(**data)print(strs3)#李白成舟将欲行,忽闻岸上踏歌声
新增方法:f方法
在字符串前加f,直接使用列表索引方法引用
data={'a':'李白','b':'踏歌'}strs3=f'{data["a"]}成舟将欲行,忽闻岸上{data["b"]}声'print(strs3)#李白成舟将欲行,忽闻岸上踏歌声
又一例
pai=3.1415926res1='pai is {:.2f}'.format(pai)res2='pai is {:.5f}'.format(pai)print(res1)#pai is 3.14print(res2)#3.14159
5、英文字符与字符检测
capitalize():返回首字母大写字符串
title():返回每个单词首字母大写
upper():全部大写
lower():全部小写
swapcase():大小写反写
strs='hello world'res=strs.capitalize()print(res)#Hello worldres2=strs.title()print(res2)#Hello Worldres3=strs.upper()print(res3)#HELLO WORLDres4=res.lower()print(res4)#hello worldres5=res2.swapcase()print(res5)#hELLO wORLD
6、字符检测
isupper():是否都是大写
islower():是否都是小写
istitle():是否满足单词首字母大写
类似还有:
startwith()
endwith()
strs='hello world'res=strs.capitalize()print(res)#Hello worldres2=strs.title()print(res2)#Hello Worldres3=strs.upper()print(res3)#HELLO WORLDres4=res.lower()print(res4)#hello worldres5=res2.swapcase()print(res5)#hELLO wORLDres6=res3.isupper()print(res6)#Trueres7=res4.islower()print(res7)#Trueres8=res2.istitle()print(res8)#True
7、字符串查找
in 检测一个字符串是否存在另一个字符串中len()字符串长度strs='hello world'aim='hello'res=aim in strsprint(res)#Trueres1=len(strs)print(res1)#11
find(sub,start,end):返回子字符串的最小索引
strs='hello world haha, niubility'aim='world'res=strs.find(aim,0,20)#最后两个或最后一个参数可以省略print(res)#6
rfind()上例从右开始查找,find默认从左查找index(sub,start,end):返回子字符串的最小索引,和find的作用相同,如果没有找到会报错,荣阳有rindex()方法count(sub,start,end):返回子字符串出现的次数
strs='hello world,hello wo, world ,helo,hello 'aim='hello'res=strs.count(aim)print(res)#3
8、字符串操作相关函数
split():按照指定的字符,把一个字符串分割成一个列表join(容器):按照指定字符,把容器内的元素组合成一个字符串strs='hello-world-hello-wo-world-helo-hello'aim='-'res=strs.split(aim)print(res)#['hello', 'world', 'hello', 'wo', 'world', 'helo', 'hello']res2='@'.join(res)print(res2)#hello@world@hello@wo@world@helo@hello
指定分割次数
strs='hello-world-hello-wo-world-helo-hello'aim='-'res=strs.split(aim,2)#只分割2次,其余字符作为下一个元素print(res)#['hello', 'world', 'hello-wo-world-helo-hello']
指定右分割
strs='hello-world-hello-wo-world-helo-hello'aim='-'res=strs.rsplit(aim,2)#只分割2次,其余字符作为下一个元素print(res)#['hello-world-hello-wo-world', 'helo', 'hello']
strip()去除头尾空格或特定字符
strs=' hello-world 'res=strs.strip()print(res)# hello-world
rstrip(),lstrip()去除左右特定字符
strs1='%%%hello%%%'res=strs1.strip()res1=strs1.strip('%')print(res1)# hellores2=strs1.rstrip('%')print(res2)#%%%hellores3=strs1.lstrip('%')print(res3)#hello%%%
replace(old,new)替换字符串
strs='hello world'aim='china'res=strs.replace('world',aim)print(res)#hello china
center(n,str):不足n,左右补充特定字符
strs='hello world'res=strs.center(20,'&')print(res)#&&&&hello world&&&&&
ljust(n,char),rjust(n,char):左补字符串,右补字符串
Python(IT峰)笔记05-数据类型详解:字符串 转义字符 字符串操作 字符串格式化 英文字符 字符串查找 字符串操作