序号 | 函数 | 作用 |
1 | len() | 返回字符串的长度 |
2 | find() | 查找字符串中指定子字符串的第一个出现的位置 |
3 | rfind() | 查找字符串中指定子字符串的最后一个出现的位置 |
4 | index() | 查找字符串中指定子字符串的第一个出现的位置,如果不存在则报错,提示异常。 |
5 | rindex() | 查找字符串中指定子字符串的最后一个出现的位置,如果不存在则报错,ti |
6 | count() | 统计字符串中指定子字符串的出现次数 |
7 | startswith() | 判断字符串是否以指定的子字符串开头 |
8 | endswith0 | 判断字符串是否以指定的子字符串結尾 |
9 | isalpha() | 判断字符串是否只包含字母字符 |
10 | isdigit() | 判断字符串是否只包含数字字符 |
11 | isalnum() | 判断字符串是否只包含字母或数字字符 |
12 | isspace() | 判断字符串是否只包含空白字符 |
13 | islower() | 判断字符串中的字母是否都为小写 |
14 | isupper() | 判断字符串中的字母是否都为大写 |
15 | istitle() | 判断字符串是否符合标题的格式 |
16 | str() | 将对象转换为字符串 |
17 | capitalize() | 将字符串的第一个字符转换为大写 |
18 | casefold() | 将字符串转换为小写,并返回对应的折叠大小写形式 |
19 | lower() | 将字符串转换为小写 |
20 | upper() | 将字符串转换为大写 |
21 | title() | 将字符串的每个单词的首字母转换为大写 |
22 | swapcase() | 将字符串中的大写字母转换为小写,小写字母转换为大写 |
23 | strip() | 去除字符串两端的空格或指定字符 |
24 | rstrip0 | 去除字符串右侧的空格或指定字符 |
25 | Istrip() | 去除字符串左侧的空格或指定字符 |
26 | replace() | 将字符串中的指定子字符串替换为另一个字符串 |
27 | split() | 将字符串按照指定分隔符分割为多个子字符串,并返回列表 |
28 | rsplit() | 将字符串按照指定分隔符从右侧开始分割为多个子字符串,并返回列表 |
29 | join() | 将字符串列表中的所有字符串连接起来,使用指定的分隔符 |
s = "hello world"# 1. 查询字符串的长度 返回int整型
print(len(s)) # 11# 2. 查找字符在字符串中第一次出现的位置 find()
print(s.find("l")) # 2
print(s.find("hello")) # 0
print(s.find("z")) # -1# 3. 查找字符在字符串中最后一次出现的位置 rfind()
print(s.rfind("d")) # 10
print(s.rfind("l")) # 9# 4. 查询字符在字符串中出现的次数 count()
print(s.count("o")) # 2
print(s.count("l")) # 3
print(s.count("z")) # 0# 5. 查询字符串中是否以某个字符开头 startswith bool
print(s.startswith("he")) # True
print(s.startswith("hello")) # True
print(s.startswith("北大")) # False# 6. 查询字符串中是否以某个字符结尾 endswith bool
print(s.endswith("d")) # True
print(s.endswith("ld")) # True
print(s.endswith("dl")) # False
print(s.endswith("world")) # Trueprint("----------------------")
# 7. 判断字符串是否只包含字母
s1 = "123"
s2 = "a123"
s3 = " "
s4 = ""
s5 = "True"
s6 = "abc,"
print(s1.isalpha()) # False
print(s2.isalpha()) # False
print(s3.isalpha()) # False
print(s4.isalpha()) # False
print(s5.isalpha()) # True
print(s6.isalpha()) # Falses7 = "Hello world, I like you, And you?"
s8 = "hello world"
s9 = "HELLO WORLD"
# 8. 判断字符串是否全部都是小写字母
print(s7.islower()) # False
print(s8.islower()) # True
print(s9.islower()) # False
# 9. 判断字符串是否全部都是大写字母
print(s7.isupper()) # False
print(s8.isupper()) # False
print(s9.isupper()) # True# 10. 判断字符串是否为标题 Hello World
s10 = "Hello World"
print(s7.istitle()) # False
print(s8.istitle()) # False
print(s9.istitle()) # False
print(s10.istitle()) # Trues11 = "Hello world, I like you"
# 11. 把字符串转换成全部小写
print(s11.lower())# 12. 把字符串转换成全部大写
print(s11.upper())# 13. 把字符串转换成全部标题
print(s11.title())# 14. 替换 replace 把旧字符串替换成新字符串 *****
# replace(old, new) old表示要替换的数据 new表示新数据
s12 = "Hello world, I like python"
print(s12.replace("python", "linux"))# 15. 切割 split() 他必须按照相同的字符进行切割, 切割成一个列表 [] *****
s13 = "hello world, i like python, i like linux"
print(s13.split(" "))s14 = "helloworld"
print(s14.split("o"))# 16. 拼接 join() 把列表通过相同的字符拼接在一起, 返回一个字符串 *****
_li = ["h", "e", "l"]
print("-".join(_li)) # h-e-l