1.查找统计类
1.1 统计字符串长度 len(xx)
print(len("abcdef"))
# 6
1.2 统计字符串中字符出现的次数 xx.count()
print("abbbbbshlisuabb".count("ab"))
# 2
1.3 查看字符第一次出现下表的位置 xx.index(),但是使用这种方法的时候如果找不到字符就会报错,导致终止程序,不推荐使用这种方法。
print("abbcd".index("b"))
# 1
1.4 查看子字符串第一次出现下标的位置 xx.find() ,如果不存在就会输出-1
print("gaigdbqbq".find("a"))
# 1
print("gaigdbqbq".find("o"))
#-1
2. 字符串的转换类
2.1 将字符串转换为大写 xx..Upper() 小写 xx.Lower() (有数字不影响,大小写不敏感的情况下使用)
print(f"将字符串转化为大写:{'dsbuhcbuvDS2352UV'.upper()}")
# 将字符串转化为大写:DSBUHCBUVDS2352UV
print(f"将字符串转化为大写:{'dsbuhcbuvDS2352UV'.lower()}")
#将字符串转化为大写:dsbuhcbuvds2352uv
2.2 字符串转化为标题格式 xx.title()
print(f"转化为标题格式:{'the world istitle'.title()}")
# 转化为标题格式:The World Istitle
2.3 字符串的切割 xx.split()
# 以h作为标志来切割字符串,遇到一个h切一刀
str1 = "hhapuichugq98yqw89y0"
print(f"字符串的切割:{str1.split('h')}")
# 字符串的切割:['', '', 'apuic', 'ugq98yqw89y0']
2.4 字符串的拼接 xx.join()
lst = ["aa", "bb", "cc"]
result = "*".join(lst)
print(result)
# aa*bb*cc
2.5去除字符串的首尾字符 xx.strip()
str2 = input("string:")
# 去除首位字符的空格
if str2.strip() == "sctl":
print(str2)
print("ok")
# 去除首尾字符的*号
if str2.strip("*") == "sctl":
print("great")
print(str2)
# 输入1
# string: sctl
# sctl
# ok
# 输入2
# string:*****sctl***
# great
# *****sctl***
2.6 字符串的替换 xx.replace("xx", "xx")
str1 = "xxxyyy".replace("x", "*")
print(str1)
# ***yyy
2.7 字符串的填充 xx.center(xx, "xx") xx.left(xx, "xx") xx.right(xx, "xx")
# 50 是代表这一行的所有字符加起来长度等于50
print("欢迎来到五米之家!".center(50, "*"))
print("1.登录".ljust(10, "*"))
print("1.登录".rjust(10, "*"))
# ********************欢迎来到五米之家!*********************
# 1.登录******
# ******1.登录