一.字符串是python基础数据类型之一,那么针对字符串,提供了很多常用的函数,用来处理字符串非常之方便
注意:在使用字符串函数的时候任何操作都不会对改变原来的字符串;因为字符串是一种不可变数据类型-这个根据id值判断
大致对字符串的操作分为以下几类:
1.改变类
2.判断类
3.查询类
4.编码格式类
5.其他处理
以下举例以s = “heLLo WorLd”为例
1.改变类:主要针对字符串的输出形式修改,不改变原字符串
s = "heLLo WorLd"
# 1.upper()-全部字符变大写
print(s.upper())
# 2.lower()-全部字符变小写
print(s.lower())
# 3.swapcase()-将大写的字母变成小写,小写的字母变成大写
print(s.swapcase())
# 4.capitalize()-将所有的字符串第一个字母变成大写
print(s.capitalize())
# 5.title()-将字符串首字母大写
print(s.title())
# 6.ljust()-获取固定的文本长度
print(s.ljust(20))
print(s.ljust(20, "*")) # 向右填充字符
# 7.rjust()-获取固定的文本长度
print(s.rjust(20, "*")) # # 向右填充字符
# 8.center()-字符串居中,两边填充
print(s.center(20, "*")) # 在两边填充,将字符串居中
# 9.zfill()-获取固定的文本长度,不够的位置用0填充
# 13.replace()-替换,将字符串中的字符替换成其他的
print(s.replace("h", "H"))
print(s.replace("L", "l")) # 默认全部替换
print(s.replace("L", "l", 1))
2.判断类:主要对字符串中字符的内容进行判断
判断类中有些字符判断如isalpha只是判断字母的,但是输入你好也能返回True,这样我们需要通过转换为字节形式才能判断成功,如下方编号33
# 14.startswitch()-判断字符串开头是以什么开头
print(s.startswith("h")) # True
print(s.startswith("H")) # False
# 15.endswitch()-判断字符串以什么结尾
print(s.endswith("d")) # True
# 18.isupper()-判断字符串是否全部大写
print(s.isupper()) # False
print("HELLO".isupper()) # True
# 19.islower()-判断字符串是否全部小写
print(s.islower()) # False
print("hello".islower()) # True
# 31.isidentifier()-判断字符串是不是合法字符
print("dfhbwes".isidentifier()) # True
# 32.isspace()-判断字符是否只有空白字符
print(" ".isspace()) # True
# 33.isalpha()-判断字符串是否全是字母:52个
print("gducvuwdsDWSIUDB".isalpha()) # True
print("gducvuwdsDWSIUDB123".isalpha()) # False
print("你好cgesdvfs".isalpha()) # True
print("你好cgesdvfs".encode().isalpha()) # False
# 34.isdigit()-判断字符串是否全是数字
print("12345".isdigit()) # True
# 35.isalnum()-判断是否全是数字和字母
print("bjcb234".isalnum()) # True
# 36.isascii()-判断字符串中的字符是否来自于ascii
print("你好".isascii()) # False
3.查询类:对字符串字符内容以及索引值进行查询
# 30.ord和chr
print(ord("h")) # 104-返回ASCII中指定字符的索引位
print(chr(104)) # h-返回ASCII中指定索引位的字符
# 10.find()-获取索引值
print(s.find("h")) # 0
print(s.find("l")) # -1 获取找不到的字符返回-1
print(s.find("L")) # 2
print(s.find("L", 3, 8)) # 3
# 11.rfind()-从右边开始获取索引值
print(s.rfind("L")) # 9
# 12.count()-计数
print(s.count("L")) # 3
# 16.index()-与find一样获取索引,但是获取不到会报错
print(s.index("h")) # 0
# print(s.index("H")) # 报错
# 17.rindex()-与rfind一样,从右边开始获取
print(s.rindex("L")) # 9
4.编码格式类
# 28.encode()-将字符串转换为指定编码格式的字节
print("hello".encode()) # b'hello',b是字节的意思
print("hello".encode("GBK")) # b'hello'
print("你好".encode("GBK")) # b'xc4xe3xbaxc3'
print("你好".encode("UTF-8")) # b'xe4xbdxa0xe5xa5xbd'
# print("你好".encode(encoding="ASCII", errors="aaa")) # 指定报错对象内容
# 29.decode()-将字节转换为指定编码格式的字符
print(b'xe4xbdxa0xe5xa5xbd'.decode("UTF-8")) # 你好
print(b'xe4xbdxa0xe5xa5xbd'.decode("GBK")) # 浣犲ソ
# print(b'xc4xe3xbaxc3'.decode("UTF-8"))
# GBK里面的字节包括了UTF-8含有的,但是UTF-8没有GBK的一些
### 同样的字节,在不同的编码格式中代表的字节不一样,甚至可能没有
# ASCIi(128) - ISO编码 - 万国码(各个国家的编码内容) - GBK - unicode(UTF-8、UTF-16)
5.其他处理
# 20.split()-切割字符串
print(s.split()) # ['heLLo', 'WorLd']
# -默认不填参数,以空格切割,并且切割之后的值是一个列表
print(s.split("L"))
# 以L切割,两个L挨在一起变成一个空格,三个L有两个空字符
print(s.split(" ")) # ['heLLo', 'WorLd']
# print(s.split("")) # 没有值会报错,ValueError: empty separator
print("dbfhsib[f busdfbuwi hlsegub shfiis".split(" ", 0))
print("dbfhsib[f busdfbuwi hlsegub shfiis".split(" ", 1))
# 后面这个参数控制切割之后截取几个值,0表示整个,计数从0开始,0算1个
# 21.rsplit()-从右边开始分割
print(s.rsplit()) # ['heLLo', 'WorLd']
print("sfbdbweu iusefg wvfi swfbi".rsplit("s"))
# 当s在首尾将变成空字符
# 22.splitlines()-按行切割
print(s.splitlines()) # ['heLLo WorLd']
print("hello n world n python".splitlines())
# ['hello ', ' world ', ' python']
print("hello n world n python".splitlines(keepends=True))
print("hello n world n python".splitlines(keepends=False))
### keepends默认值为False,当改成True时,字符串中的换行将当做字符处理
# 23.partition()-找到字符串中第一个str,以他为界分割三部分,返回一个元组
print(s.partition("he"))
# 24.rpartition()
print(s.rpartition("L")) # 以他为界不是以他切割
# 25.join()-拼接
list01 = ["hello", "world"]
str01 = " ".join(list01) # 以空格将字符串拼接起来
print(str01) # hello world
str02 = "-".join(list01)
print(str02) # hello-world
# 26.strip()--默认清除字符串前后的空格
print(" dbvwi ".strip())
print(" dvrfeb ewvobuer".strip("r"))
# 按指定字符去前后清除,不能清除中间的
print("!@#$%^&**&^%$12#@".strip("!@#$21"))
# %^&**&^%-原理:每一个字符都独立的去前后遍历
# 如果清除过程中前后含有对应字符就清除,前后都终止则清除结束
# 27.rstrip()--只清除右边的
print("!@#$%^&**&^%$12#@".rstrip("!@#$21")) # !@#$%^&**&^%
有了以上这些字符串的常用操作,可以很方便的帮助我们在日常开发或写脚本过程中省略很多事儿,字符串的处理基本上在工作中会占大头;当然除了这些还有一些比较冷门的字符串常用函数,并且这样的函数我们自己也可以进行封装


