python字符串的方法
| 方法 | 用法 |
|---|
| capitalize | 字符串首字母大写 |
| title | 每个单词的首字母大写 (非字母隔开的单词) |
| upper | 将所有字母变成大写 |
| swapcase | 大小写互换 |
| count | 统计字符串中某个元素的数量 |
| find | 查找某个字符串第一次出现的索引位置 |
| index | 与 find 功能相同 find找不到返回-1,index找不到数据直接报错 |
| startswith | 判断是否以某个字符或字符串为开头 |
| endswith | 判断是否以某个字符或字符串结尾 |
| split | 按某字符将字符串分割成列表(默认从左到右按空格分割) |
| join | 按某字符将列表拼接成字符串(容器类型都可) |
| replace | 替换字符串(第三个参数选择替换的次数) |
# capitalize 字符串首字母大写
s = "hello world!"
res = s.capitalize()
print(res) # Hello world!
# title 每个单词的首字母大写 (非字母隔开的单词)
s = "xiao ming"
res = s.title()
print(res) # Xiao Ming
ss = "xiao20ming" # 用数字隔开
res = ss.title()
print(res) # Xiao20Ming
# upper 将所有字母变成大写
s = "da xie"
res = s.upper()
print(res) # DA XIE
# lower 将所有字母变成小写
s = "XIAO Xie"
res= s.lower()
print(res) # xiao xie
# swapcase 大小写互换
s = "HU huan"
res = s.swapcase()
print(res) # hu HUAN
# count 统计字符串中某个元素的数量
s = "fdjzdcdsssjcdhkxnashzd"
res = s.count("s") # 默认从索引0开始统计
print(res) # 4
res = s.count("s",15) # 从索引15处开始统计
print(res) # 1
res = s.count("s",0,9) # 在索引0-8之间统计 [0,9)是一个左闭右开区间
print(res) # 2
# find 查找某个字符串第一次出现的索引位置
s = "01234560123456"
res = s.find("1") # 默认从索引0开始查找
print(res) # 1
res = s.find("1",3) # 默认从索引3开始查找
print(res) # 8
res = s.find("1",3,6) # 在索引3-6之间查找 [3,6)是一个左闭右开区间
print(res) # -1 没有返回-1
# index 与 find 功能相同 find找不到返回-1,index找不到数据直接报错
s = "01234560123456"
res = s.index("2")
print(res) # 2
# res = s.index("9")
# print(res) # ValueError: substring not found ,报错
# startswith 判断是否以某个字符或字符串为开头
s = "hello woeld!"
res = s.startswith("he") # 默认从索引0开始判断
print(res) # True
res = s.startswith("he",2) # 从索引2开始判断
print(res) # False
res = s.startswith("he",0,1) # # 在索引0-1之间判断 [0,1)是一个左闭右开区间
print(res) # False
# endswith 判断是否以某个字符或字符串结尾,用法和startswith一样
s = "hello woeld"
res = s.endswith("ld")
print(res) # True
res = s.endswith("ld",-1)
print(res) # False
res = s.endswith("ld",-5,-1)
print(res) # False
# split 按某字符将字符串分割成列表(默认从左到右按空格分割,返回一个列表)
s = "a b c d e"
res = s.split() # 默认空格分割
print(res) # ['a', 'b', 'c', 'd', 'e']
s = "a+b+c+d+e"
res = s.split("+") # 用+号分割,结果没有分割字符
print(res) # ['a', 'b', 'c', 'd', 'e']
res = s.split("+",2) # 用+号分割,分割两次
print(res) # ['a', 'b', 'c+d+e']
# join 将列表拼接成字符串(容器类型都可)
ls = ['0','1','2','3','4','5','6']
res = ''.join(ls) # 默认无缝拼接,列表元素必须是字符串
print(res) # 0123456
res = '+'.join(ls) # 用+号拼接
print(res) # 0+1+2+3+4+5+6
# replace 替换字符串(第三个参数选择替换的次数)
s = "a1 b1 c1 a2 b2 c2 a1 a1"
res = s.replace("a1","aa") # 将所有的a1都转换成aa
print(res) # aa b1 c1 a2 b2 c2 aa aa
# 第三个参数指定转换的次数
res = s.replace("a1","aa",1) # 将一个a1转换成aa
print(res) # aa b1 c1 a2 b2 c2 a1 a1
| 方法 | 用法 |
|---|
| isupper | 判断字符串是否都是大写字母 |
| islower | 判断字符串是否都是小写字母 |
| istitle | 判断字符串是否每个单词都首字母大写 |
| isalnum | 判断字符串是否是由数字、字母、文字组成 |
| isalpha | 判断字符串是否由字母和文字组成 |
| isdigit | 检测字符串数是数字组成 接受二进制字节流 |
| isdecimal | 检测字符串是否以数字组成 必须是纯数字 |
| isnumeric | 检测字符串是否以数字组成 接受中文"四" |
| isspace | 判断字符串是否由空白符组成 |
# isupper 判断字符串是否都是大写字母
s = "AABB"
ss = "Ab"
print(s.isupper()) # True
print(ss.isupper()) # False
# islower 判断字符串是否都是小写字母
s = "ab"
ss = "Ab"
print(s.islower()) # True
print(ss.islower()) # False
| 方法 | 用法 |
|---|
| splitlines | 按换行来进行切分(n) |
| zfill | 填充字符串(默认填充0,原字符串右对齐) |
| ljust | 填充字符串,原字符居左 (默认填充空格) |
| rjust | 填充字符串,原字符居右 (默认填充空格) |
| center | 填充字符串,原字符居中 (默认填充空格) |
| strip | 默认去掉首尾两边的空白符 |
| rstrip | 去掉右边某个字符 |
| lstrip | 去掉左边某个字符 |
# splitlines 按换行来进行切分(n)
s = '111n222'
print(s.splitlines()) # ['111', '222']
# zfill 填充字符串(默认填充0,原字符串右对齐)
s = "哈哈"
print(s.zfill(10)) # 00000000哈哈
# ljust 填充字符串,原字符居左 (默认填充空格)
s = "哈哈"
print(s.ljust(20,"*")) # 哈哈******************
# rjust 填充字符串,原字符居右 (默认填充空格)
s = "哈哈"
print(s.rjust(20,"+")) # ++++++++++++++++++哈哈
# center 填充字符串,原字符居中 (默认填充空格)
s = "哈哈"
print(s.center(20,"-")) # ---------哈哈---------