009.Python字符串相关函数

 


字符串相关函数

1 capitalize 字符串首字母大写 

strvar = "this is a dog"
res = strvar.capitalize()
print(res)

执行

[root@node10 python]# python3 test.py
This is a dog

2 title 每个单词的首字母大写 

非字母隔开的单词

strvar = "this is123a dog"
res = strvar.title()
print(res)

执行

[root@node10 python]# python3 test.py
This Is123A Dog

3 upper 将所有字母变成大写

strvar = "A C c d"
res = strvar.upper()
print(res)

执行

[root@node10 python]# python3 test.py
A C C D

4 lower 将所有字母变成小写

strvar = "A C c d"
res = strvar.lower()
print(res)

执行

[root@node10 python]# python3 test.py
a c c d

5 swapcase 大小写互换 

strvar = "A C c d"
res = strvar.swapcase()
print(res)

执行

[root@node10 python]# python3 test.py
a c C D

6 count 统计字符串中某个元素的数量 

strvar = "aa bbccfsdfkjak"
res = strvar.count("a")
print(res)

执行

[root@node10 python]# python3 test.py
3

7 find 查找某个字符串第一次出现的索引位置 

复制代码
strvar = "oh Father this is My Favarate boY"
res = strvar.find("F")    # 3
print(res)
res = strvar.find("F",4) # 21 print(res)
res = strvar.find("F",10,20) print(res)
res = strvar.find("aa") print(res)
复制代码

执行

[root@node10 python]# python3 test.py
3
21
-1
-1

#index 与 find 功能相同 find找不到返回-1,index找不到数据直接报错
# res = strvar.index("aa") # 推荐使用find

8 startswith 判断是否以某个字符或字符串为开头

复制代码
strvar = "oh Father this is My Favarate boY"
res= strvar.startswith("oh")
print(res)
res = strvar.startswith("thi",10) # True
print(res)
res = strvar.startswith("thi",10,12)
print(res)
复制代码

执行

[root@node10 python]# python3 test.py
True
True
False

9 endswith 判断是否以某个字符或字符串结尾 

strvar = "oh Father this is My Favarate boY"
res = strvar.endswith("boY")
print(res)
res = strvar.endswith("bo",-4,-1) #  bo
print(res)

执行

[root@node10 python]# python3 test.py
True
True

10 split 按某字符将字符串分割成列表(默认从左到右按空格分割)

复制代码
strvar = "you can you up no can no bb"
res = strvar.split()
print(res)
strvar = "you=can=you=up=no=can=no=bb"
res = strvar.split("=",2) # 第二个参数是分割几次 (从左向右)
print(res)
# rsplit 从右向左分割
res = strvar.rsplit("=",1) # (从右向左)
print(res) # 返回列表
复制代码

执行

11 join  按某字符将列表拼接成字符串(容器类型都可)

listvar  =  ['you', 'can',"a","basestring"]
res = "*".join(listvar)
print(res)  # 返回字符串

执行

[root@node10 python]# python3 test.py
you*can*a*basestring

12 replace 替换字符串(第三个参数选择替换的次数)

复制代码
strvar = "可爱的小狼狗喜欢吃肉,有没有,有没有,还有没有"
res  = strvar.replace("有没有","真没有")
print(res)
# 第三个参数为替换的次数
res = strvar.replace("有没有","真没有",1)
print(res)
复制代码

执行

[root@node10 python]# python3 test.py
可爱的小狼狗喜欢吃肉,真没有,真没有,还真没有
可爱的小狼狗喜欢吃肉,真没有,有没有,还有没有

13  isdecimal 检测字符串是否以数字组成  必须是纯数字

res = "11323"
print(res.isdecimal())
res = "1132....3"
print(res.isdecimal())

执行

[root@node10 python]# python3 test.py
True
False

14 len 计算容器类型长度

res = len("aabbcc")
print(res)

执行

[root@node10 python]# python3 test.py
6

15 center 填充字符串,原字符居中 (默认填充空格)

strvar = "你好"
res = strvar.center(10,"*") #center(填充的个数,填充的字符)
print(res)

执行

[root@node10 python]# python3 test.py
****你好****

16 strip  默认去掉首尾两边的空白符 

strvar = "\r sdf   \t \n"
print(strvar)
res = strvar.strip()
print(res)

执行

[root@node10 python]# python3 test.py
 sdf

sdf
posted @ 2021-01-24 21:18  uplee  阅读(80)  评论(0编辑  收藏  举报