*capitalize 字符串首字母大写
strvar ="how are you"
res = strvar.capitalize()# 前提strvar必须是字符串print(res)
How are you
*title 每个单词的首字母大写
strvar ="how old are you"
res = strvar.title()print(res)
How Old Are You
*upper 将所有字母变成大写
strvar ="how old are you"
res = strvar.upper()print(res)
HOW OLD ARE YOU
*lower 将所有字母变成小写
strvar ="HOW OLD ARE YOU"
res = strvar.lower()print(res)
how old are you
*swapcase 大小写互换 # 小写变大写 大写变小写
strvar ="How old Are You"
res = strvar.swapcase()print(res)
hOW OLD aRE yOU
*len 计算字符串的长度
strvar ="python"
res =len(strvar)print(res)6*count 统计字符串中某个元素的数量
"""count(字符,[开始值,结束值])"""# 下标 0 1 2 3 4 5 6
strvar ="你真好好好好啊"
res = strvar.count("好")# 统计字符串中的"好"的数量print(res)4
res = strvar.count("好",5)# 指定下标为5,设置开始范围print(res)1
res = strvar.count("真",2,3)# 指定开始下标2,结束下标3print(res)0*find 查找某个字符串第一次出现的索引位置 (推荐)"""find(字符,[开始值,结束值])"""
strvar ="To be or not to be that is a question"
res = strvar.find("to")# 显示出字符串to第一次出现的下标索引print(res)13
res = strvar.find("be",4)# 指定下标为4,设置开始范围print(res)16
res = strvar.find("be",4,10)# # 指定开始下标2,结束下标3print(res)-1# find在当前指定的区间找不到返回字符串,返回-1*index
与 find 功能相同,find找不到返回-1,index找不到数据直接报错
strvar ="To be or not to be that is a question"
res = strvar.index("to")# 显示出字符串to第一次出现的下标索引print(res)13
res = strvar.index("be",4)# 指定下标为4,设置开始范围print(res)16
strvar ="To be or not to be that is a question"
res = strvar.index("be",4,10)# 指定开始下标2,结束下标3print(res)# ValueError: substring not found 找不到,报错*startswith 判断是否以某个字符或字符串为开头
"""
语法一致
startswith(字符,[开始值,结束值])
endswith(字符,[开始值,结束值])
都是从左到右,没有间隔值
"""
strvar ="To be or not to be that is a question"
res = strvar.startswith("To")# 判断字符串是否以To开头,是True,不是Falseprint(res)True
res = strvar.startswith("To",10)# 指定下标为10,设置开始范围print(res)
Flase
res = strvar.startswith("to",13)# 指定下标为10,设置开始范围print(res)True*endswith 判断是否以某个字符或字符串结尾
strvar ="To be or not to be that is a question"
res = strvar.endswith("question")print(res)True
res = strvar.endswith("is",-14,-11)# isprint(res)True
res = strvar.endswith("is",3,10)# be or nprint(res)False
res = strvar.endswith("that",-21,-14)# be thatprint(res)Trueis系列
# 返回的不是真 即是假*isupper 判断字符串是否都是大写字母 # 加中文和符号不影响,只判断大小写
strvar ="HOW A YOU"
res = strvar.isupper()print(res)True
strvar ="HOW are YOU"
res = strvar.isupper()print(res)False*islower 判断字符串是否都是小写字母
strvar ="asdf - as"
res = strvar.islower()print(res)True*isdecimal 检测字符串是否以数字组成 必须是纯数字
strvar ="abcdefg"
res = strvar.isdecimal()print(res)False# 不是数字
strvar ="2134234.123"
res = strvar.isdecimal()print(res)False# 不是纯数字
strvar ="2134234"
res = strvar.isdecimal()print(res)
核心系列
*split 按某字符将字符串分割成列表(默认字符是空格)# 将字符串分割为列表
strvar ="you can you up no can no say"
lst = strvar.split()# 默认以空格分割,不需要指定分隔符print(lst)
strvar ="you#can#you#up#no#can#no#say"# 用#分割
lst = strvar.split("#")# 指定分割为#print(lst)['you','can','you','up','no','can','no','say']
strvar ="python"
lst = strvar.split()
strvar ="p#y#th#no"
lst = strvar.split("#")print(lst)['p','y','th','no']*join 按某字符将列表拼接成字符串(容器类型都可)# 将列表拼接为字符串
lst =['you','can','you','up','no','can','no', 'say
strvar =" ".join(lst)print(strvar)
you can you up no can no say
strvar ="#".join(lst)print(strvar)
you#can#you#up#no#can#no#say*replace 把字符串的旧字符换成新字符
"""字符串.replace('旧字符','新字符'[, 限制替换的次数])"""
strvar ="买,不买,买,不买,买,不买"
res = strvar.replace("不买","买")# 全部替换print(res)
买,买,买,买,买,买
# 选择替换的次数
res = strvar.replace("不买","买",1)# 指定替换次数,从左到右print(res)
买,买,买,不买,买,不买
*strip 默认去掉首尾两边的空白符
"""空白符 空格 \n \t \r ... """
strvar =" 周润发 "
res = strvar.strip()print(strvar)print(res)
周润发
周润发
rstrip 去掉右边某个字符
strvar ="python"
res = strvar.rstrip("n")print(res)
pytho
lstrip 去掉左边某个字符
strvar ="python"
res = strvar.lstrip("py")print(res)
thon
*center 填充字符串,原字符居中 (默认填充空格)"""center(字符长度,填充符号)"""
strvar ="python"
res = strvar.center(10)# res = strvar.center(10,"*")print(res)
python
strvar ="python"
res = strvar.center(10,"$")print(res)
$$python$$