云计算百科
云计算领域专业知识百科平台

python字符串方法大合集

最近我学习了bilibili上小甲鱼的《零基础入门学习python》以及阅读了书籍《python编程——从入门到实践》学习到了字符串的一些方法,但这些方法太多也太过于冗杂,我一时半会记不了这么多,也不能隆回贯通的使用,于是打算用该文章总结一下这些方法。

目录

一、python字符串的大小写转换

二、去除python字符串的空白/指定字符

三、python字符串的对齐和补充

四、python中查找和计数的方法


一、python字符串的大小写转换

序号方法作用示例
x.upper()

将所有字母换成大写

x="Hello Python"
print(x.upper())
#HELLO PYTHON
x.lower() 将所有字母全部换成小写(兼容多种语言) x="Hello Python"
print(x.lower())
#hello python
x.capitalize() 只有整句话的首写字母大写,其余字母全部换成小写 x="Hello Python"
print(x.capitalize())
#Hello python
x.title() 每个单词首字母大写 x="Hello Python"
print(x.title())
#Hello Python
x.swapcase() 所有字母的大小写互换 x="Hello Python"
print(x.swapcase())
#hELLO pYTHON
x.casefold() 所有字母全部换成小写(只兼容英文字母) x="Hello Python"
print(x.casefold())
#hello python

二、去除python字符串的空白/指定字符

序号 方法 作用 示例 误区
x.strip(chars) 去除字符串左右两边的空白字符(空格,\\n,\\t,\\r等),可以指定某个字符 x=" abc "
print(x.strip())
#"abc"
x="123abc321"
print(x.strip("123"))
#"abc"
strip("123")不只是删除完整字符串“123”,而是逐个字符串判断只要字符串符合‘1’,‘2’,‘3’就将其删掉
x.lstrip(chars) 去除字符串左侧的空白字符,可以指定某个字符 x=" abc "
print(x.lstrip())
#"abc "
x="123abc321"
print(x.lstrip("123"))
#"abc321"

同上
x.rstrip(chars) 去除字符串右侧的空白字符,可以指定某个字符 x=" abc "
print(x.rstrip())
#" abc"
x="123abc321"
print(x.rstrip("123"))
#"123abc"
同上
x.removeprefix(prefix) 去除开头固定字符串 x="123abc321"
print(x.removefix("123"))
#"abc321"

括号内的内容一定要和要删除的内容一致(顺序,空格等都要一致)

python3.9+新增方法

x.removesuffix(suffix) 去除结尾固定字符串 x="123abc321"
print(x.removesuffix("321"))
#"123abc"

同上

python3.9+新增方法

三、python字符串的对齐和补充

序号 方法 作用 示例
x.center(width,fillchar) 居中对齐,总宽度为width,用指定字符fillchar填充空格 x="123"
print(x.center(11,"*"))
#"****123****"
x.ljust(width,fillchar) 左对齐 x="123"
print(x.ljust(6,"-"))
#"123—"
x.rjust(width,fillchar) 右对齐 x="123"
print(x.rjust(6,"&"))
#"&&&123"
x.zfill(width) 右侧补0(专门用于数字字符串) x="123"
print(x.zfill(6))
#"000123"

四、python中查找和计数的方法

序号 方法 作用 示例
x.find(sub,start,end)

从左往右找

找到,返回第一匹配项的索引值

找不到,返回-1,不会报错

-sub要查找的字符串

-start、end查找的范围[左闭,右开)

x="hello world"
print(x.find("l",2,10))
#3
x="hello world"
print(x.find("a",2,10))
#-1
x.rfind(sub,star,end) 从右往左找 x="hello world"
print(x.rfind("l",2,10))
#9
x="hello world"
print(x.rfind("a",2,10))
#-1
x.index(sub,star,end) 功能和find基本一致,只是找不到字符串会报错 x="hello world"
print(x.index("l",2,10))
#3
x="hello world"
print(x.index("a",2,10))
#报错
x.rindex(sub,star,end) 功能和rfind基本一致,只是找不到字符串会报错 x="hello world"
print(x.rindex("l",2,10))
#9
x="hello world"
print(x.rindex("a",2,10))
#报错
x.count(sub,star,end) 某字符串出现的次数,没有就返回0,不会报错 x="hello,world"
print(x.count("l",0,len(x)))
#3
x="hello world"
print(x.count("a"))
#0

五、python的字符串替换

序号 方法 作用 示例
x.replace(old,new,count)

用新的字符串代替旧的字符串

-old要被替代的旧字符串

-new用来替代的新字符串

-count替换次数,一般默认为-1,代表全部替换

x="aa bb cc aa"
print(x.replace("aa","dd"))
#"dd bb cc dd"
x="aa bb cc aa"
print(x.replace("aa","dd",1))
#"dd bb cc aa"
x.expandtabs(tabsize)

将\\t换成固定的空格(空格数=指定空格数-\\t前面的字符数)

-tabsize的默认值为8

x="hello\\tpython"
print(x.expandtabs())
#"hello python"
x="hello\\tpython"
print(x.expandtabs(6))
#"hello python"

六、python字符串的分割与合并

序号 方法 作用 示例
x.split(sep,maxsplit)

把字符串分割并形成列表

-sep分隔符,默认sep=None就是分割所有的空白(空格,\\t,\\n)

-maxsplit指分割次数,默认值为-1,代表全部分割,连续的空白算一个,只分割一次

x="a-b-c-d"
print(x.split("-"))
#['a','b','c','d']
x="a-b-c-d"
print(x.split("-",2))
#['a','b','c-d']
x.rsplit(sep,maxsplit) 从右侧开始分割字符串 x="a-b-c-d"
print(x.rsplit("-"))
#['a','b','c','d']
x="a-b-c-d"
print(x.rsplit("-",2))
#['a-b','c','d']
x.partition(sep)

只能分割一次,并形成元组,分割符不能省略,若查找不到相应的分割符,将会用“,”替代

x="a-b-c-d"
print(x.partition("-"))
#('a','-','b-c-d')
x="a-b-c-d"
print(x.partition("*"))
#('a-b-c-d',",")
x.rpartition(sep) 只能从右侧分割一次 x="a-b-c-d"
print(x.rpartition("-"))
#('a-b-c','-','d')
x="a-b-c-d"
print(x.rpartition("*"))
#('a-b-c-d',",")
x.splitlines(keepends)

按照换行符(\\n,\\r,\\r\\n)分割,并将字符串转为列表

-keepends默认值为False意为丢掉换行符,当keepends为True时,保留换行符

x="aa\\nbb\\rcc"
print(x.splitlines())
#['aa','bb','cc']
x="aa\\nbb\\rcc"
print(x.splitlines(True))
#['aa\\n','bb\\r','cc']
分隔符.join(可迭代对象)

将列表拼接为字符串,和split作用相反

-可迭代对象:列表、元组等,里面的元素必须全部是字符串,不能是数字

x=["hello","world"]
print("-".join(x))
#"hello-world"

七、python的判断类方法(返回布尔值True/False)

序号 方法 作用 示例
x.islower() 判断字符串的所有英文字母是否全部小写(汉字、数字、符号不影响判断) x="hello"
print(x.islower())
#True
x.isupper() 判断字符串的所有英文字母是否全部大写 x="HELLO"
print(x.isupper())
#True
x.istitle() 判断字符串的每个单词首字母大写,其余字母小写 x="Hello Python"
print(x.istitle())
#True
x.isalpha() 判断字符串全部由汉字/字母构成,不能包含数字、空格、符号 x="今天是个好天气"
print(x.isalpha())
#True
x.isdigit() 判断字符串只包含阿拉伯数字(0-9),汉字数字、阿拉伯数字等直接返回False x="2026"
print(x.isdigit())
#True
x="3²"
print(x.isdigit())
#True
x="二零二六"
print(x.isdigit())
#False
x.isdecimal() 仅支持十进制阿拉伯数字,是三种判断数字类型里面最严格的 x="2026"
print(x.isdecimal())
#True
x="3²"
print(x.isdecimal())
#False
x.isnumeric() 判断字符串数字类型中范围最大 x="2026"
print(x.isnumeric())
#True
x="二零二六"
print(x.isnumeric())
#True
x="3²"
print(x.isnumeric())
#True
x.isalnum() 判断字符串全部由字母+汉字+数字组成,无空格,标点符号 x="123abc"
print(x.isalnum())
#True
x.isspace() 判断整个字符串全部由空白字符构成(空格、\\n,\\t,\\r等),至少要有一个空白 x=" \\n"
print(x.isspace())
#True
x.startswith(perfix,start,end) 判断字符串是否以指定字符开头,可以限定索引范围 x="hello python"
print(x.startswith("he"))
#True
x="hello python"
print(x.startswith("py",6,len(x)))
#True
x.endswith(suffix,start,end) 判断字符串是否已以指定字符结尾 x="hello python"
print(x.endswith("on"))
#True
x="hello python"
print(x.endswith("lo",0,5))
#True

八、python编码和解码

序号 方法 作用 示例
x.encode(encoding,errors)

将字符串转化成字节(str→bytes)

-encoding编码格式,常用utf-8、gbk

-errors默认为strict,遇到无法编码的字符串直接报错,当输入ignore时,直接忽略不能编码的字符;当输入replace时,用?替换无法编码的字符

x="中国"
print(x.encode("utf-8"))
#'\\xe4\\xb8\\xad\\xe5\\x9b\\xbd'
x.decode(encoding,errors) 作用与encode相反(bytes→str) x='\\xe4\\xb8\\xad\\xe5\\x9b\\xbd'
print(x.decode("utf-8"))
#"中国"

如果该文章有什么错误的地方请及时告诉,我会立即改正!

赞(0)
未经允许不得转载:网硕互联帮助中心 » python字符串方法大合集
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!