python字符串方法大合集

📅 2026/7/16 9:19:04
python字符串方法大合集
最近我学习了bilibili上小甲鱼的《零基础入门学习python》以及阅读了书籍《python编程——从入门到实践》学习到了字符串的一些方法但这些方法太多也太过于冗杂我一时半会记不了这么多也不能隆回贯通的使用于是打算用该文章总结一下这些方法。目录一、python字符串的大小写转换二、去除python字符串的空白/指定字符三、python字符串的对齐和补充四、python中查找和计数的方法一、python字符串的大小写转换序号方法作用示例①x.upper()将所有字母换成大写xHello Python print(x.upper()) #HELLO PYTHON②x.lower()将所有字母全部换成小写兼容多种语言xHello Python print(x.lower()) #hello python③x.capitalize()只有整句话的首写字母大写其余字母全部换成小写xHello Python print(x.capitalize()) #Hello python④x.title()每个单词首字母大写xHello Python print(x.title()) #Hello Python⑤x.swapcase()所有字母的大小写互换xHello Python print(x.swapcase()) #hELLO pYTHON⑥x.casefold()所有字母全部换成小写只兼容英文字母xHello Python print(x.casefold()) #hello python二、去除python字符串的空白/指定字符序号方法作用示例误区①x.strip(chars)去除字符串左右两边的空白字符空格\n\t\r等可以指定某个字符x abc print(x.strip()) #abc x123abc321 print(x.strip(123)) #abcstrip(123)不只是删除完整字符串“123”而是逐个字符串判断只要字符串符合‘1’‘2’‘3’就将其删掉②x.lstrip(chars)去除字符串左侧的空白字符可以指定某个字符x abc print(x.lstrip()) #abc x123abc321 print(x.lstrip(123)) #abc321同上③x.rstrip(chars)去除字符串右侧的空白字符可以指定某个字符x abc print(x.rstrip()) # abc x123abc321 print(x.rstrip(123)) #123abc同上④x.removeprefix(prefix)去除开头固定字符串x123abc321 print(x.removefix(123)) #abc321括号内的内容一定要和要删除的内容一致顺序空格等都要一致python3.9新增方法⑤x.removesuffix(suffix)去除结尾固定字符串x123abc321 print(x.removesuffix(321)) #123abc同上python3.9新增方法三、python字符串的对齐和补充序号方法作用示例①x.center(width,fillchar)居中对齐总宽度为width用指定字符fillchar填充空格x123 print(x.center(11,*)) #****123****②x.ljust(width,fillchar)左对齐x123 print(x.ljust(6,-)) #123---③x.rjust(width,fillchar)右对齐x123 print(x.rjust(6,)) #123④x.zfill(width)右侧补0专门用于数字字符串x123 print(x.zfill(6)) #000123四、python中查找和计数的方法序号方法作用示例①x.find(sub,start,end)从左往右找找到返回第一匹配项的索引值找不到返回-1不会报错-sub要查找的字符串-start、end查找的范围[左闭右开xhello world print(x.find(l,2,10)) #3 xhello world print(x.find(a,2,10)) #-1②x.rfind(sub,star,end)从右往左找xhello world print(x.rfind(l,2,10)) #9 xhello world print(x.rfind(a,2,10)) #-1③x.index(sub,star,end)功能和find基本一致只是找不到字符串会报错xhello world print(x.index(l,2,10)) #3 xhello world print(x.index(a,2,10)) #报错④x.rindex(sub,star,end)功能和rfind基本一致只是找不到字符串会报错xhello world print(x.rindex(l,2,10)) #9 xhello world print(x.rindex(a,2,10)) #报错⑤x.count(sub,star,end)某字符串出现的次数没有就返回0不会报错xhello,world print(x.count(l,0,len(x))) #3 xhello world print(x.count(a)) #0五、python的字符串替换序号方法作用示例①x.replace(old,new,count)用新的字符串代替旧的字符串-old要被替代的旧字符串-new用来替代的新字符串-count替换次数一般默认为-1代表全部替换xaa bb cc aa print(x.replace(aa,dd)) #dd bb cc dd xaa bb cc aa print(x.replace(aa,dd,1)) #dd bb cc aa②x.expandtabs(tabsize)将\t换成固定的空格(空格数指定空格数-\t前面的字符数)-tabsize的默认值为8xhello\tpython print(x.expandtabs()) #hello python xhello\tpython print(x.expandtabs(6)) #hello python六、python字符串的分割与合并序号方法作用示例①x.split(sep,maxsplit)把字符串分割并形成列表-sep分隔符默认sepNone就是分割所有的空白空格\t,\n-maxsplit指分割次数默认值为-1代表全部分割连续的空白算一个只分割一次xa-b-c-d print(x.split(-)) #[a,b,c,d] xa-b-c-d print(x.split(-,2)) #[a,b,c-d]②x.rsplit(sep,maxsplit)从右侧开始分割字符串xa-b-c-d print(x.rsplit(-)) #[a,b,c,d] xa-b-c-d print(x.rsplit(-,2)) #[a-b,c,d]③x.partition(sep)只能分割一次并形成元组分割符不能省略若查找不到相应的分割符将会用“”替代xa-b-c-d print(x.partition(-)) #(a,-,b-c-d) xa-b-c-d print(x.partition(*)) #(a-b-c-d,,)④x.rpartition(sep)只能从右侧分割一次xa-b-c-d print(x.rpartition(-)) #(a-b-c,-,d) xa-b-c-d print(x.rpartition(*)) #(a-b-c-d,,)⑤x.splitlines(keepends)按照换行符\n,\r,\r\n分割,并将字符串转为列表-keepends默认值为False意为丢掉换行符当keepends为True时保留换行符xaa\nbb\rcc print(x.splitlines()) #[aa,bb,cc] xaa\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()判断字符串的所有英文字母是否全部小写汉字、数字、符号不影响判断xhello print(x.islower()) #True②x.isupper()判断字符串的所有英文字母是否全部大写xHELLO print(x.isupper()) #True③x.istitle()判断字符串的每个单词首字母大写其余字母小写xHello Python print(x.istitle()) #True④x.isalpha()判断字符串全部由汉字/字母构成不能包含数字、空格、符号x今天是个好天气 print(x.isalpha()) #True⑤x.isdigit()判断字符串只包含阿拉伯数字0-9汉字数字、阿拉伯数字等直接返回Falsex2026 print(x.isdigit()) #True x3² print(x.isdigit()) #True x二零二六 print(x.isdigit()) #False⑥x.isdecimal()仅支持十进制阿拉伯数字是三种判断数字类型里面最严格的x2026 print(x.isdecimal()) #True x3² print(x.isdecimal()) #False⑦x.isnumeric()判断字符串数字类型中范围最大x2026 print(x.isnumeric()) #True x二零二六 print(x.isnumeric()) #True x3² print(x.isnumeric()) #True⑧x.isalnum()判断字符串全部由字母汉字数字组成无空格标点符号x123abc print(x.isalnum()) #True⑨x.isspace()判断整个字符串全部由空白字符构成空格、\n,\t,\r等至少要有一个空白x \n print(x.isspace()) #True⑩x.startswith(perfix,start,end)判断字符串是否以指定字符开头可以限定索引范围xhello python print(x.startswith(he)) #True xhello python print(x.startswith(py,6,len(x))) #True⑪x.endswith(suffix,start,end)判断字符串是否已以指定字符结尾xhello python print(x.endswith(on)) #True xhello python print(x.endswith(lo,0,5)) #True八、python编码和解码序号方法作用示例①x.encode(encodingerrors)将字符串转化成字节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→strx\xe4\xb8\xad\xe5\x9b\xbd print(x.decode(utf-8)) #中国如果该文章有什么错误的地方请及时告诉我会立即改正