python 字符串中常见的内置函数
001、 capitalize将字符串的首字符转换为大写, 其余全部转换为小写
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str1.capitalize() ## 将字符串首字符转换为大写, 其余全部转换为小写 'Aabbcceeff'
002、 casefold将字符串中的字符全部转换为小写字母
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str1.casefold() ## 将字符串中的所有字符都转换为小写字母 'aabbcceeff'
003、lower 将字符串的所有字符都转换为小写字符
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str1.lower() ## 将字符串中的所有字符都转换为小写字符 'aabbcceeff'
004、upper将字符串中的所有字符都转换为大写字符
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str1.upper() ## 将字符串中的所有字符都转换为大写字母 'AABBCCEEFF'
005、swapcase 将大写字母转换小写字母,将小写字母转换为大写字母
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str1.swapcase() ## 将大写字母转换为小写字母,将小写字母转换为大写字母 'AAbbCCeeFF'
006、isupper 判断字符串是否全部由大写字母组成
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str2 = "AABBCCDD" >>> str2 'AABBCCDD' >>> str1.isupper() ## 判断字符串是否全部有大写字母组成 False >>> str2.isupper() True
007、islower判断字符串是否全部由小写字母组成
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str2 = "aabbccdd" >>> str2 'aabbccdd' >>> str1.islower() ## 判断字符串是否全部由小写字符组成 False >>> str2.islower() True
008、istitile 判断字符串是否首字符为大写同时其余为小写
>>> str1 = "aaBBccEEff" >>> str1 'aaBBccEEff' >>> str2 = "Adfst" >>> str2 'Adfst' >>> str1.istitle() ## 判断字符串是否首字符为大写,其余为小写 False >>> str2.istitle() True
009、count统计字符串中指定字符的次数
>>> str1 = "aabbaaccdddb" >>> str1.count("a") ## 统计指定字符在字符串中出现的次数 4 >>> str1.count("a", 3) ## 从索引为3的位置检索 2 >>> str1.count("a", 0, 3) ## 指定范围 2
010、center将字符串居中排列
>>> str1 = "abcd" >>> str1 'abcd' >>> str1.center(6) ## 将字符串居中排列 ' abcd ' >>> str1.center(10) ' abcd ' >>> str1.center(10, "*") ## 指定宽度,并指定填充字符 '***abcd***'
011、startswith 判断字符串是否以指定字符开头
>>> str1 = "abcd" >>> str1 'abcd' >>> str1.startswith("a") ## 判断字符串是否以指定字符开头 True >>> str1.startswith("x") False
>>> str1 = "abcd" >>> str1 'abcd' >>> str1.startswith("c") False >>> str1.startswith("c", 2) ## 判断字符串是否以指定字符串开头, 可以指定检索的范围 True
012、endswith 判断字符串是否以指定字符结尾
>>> str1 = "abcd" >>> str1 'abcd' >>> str1.endswith("d") ## 判断字符串是否以指定字符结尾 True >>> str1.endswith("b") False>>> str1.endswith("b", 0, 2) ## 可以指定检索的范围 True
013、find 查找字符串中自定的字符
>>> str1 = "abcderabed" >>> str1 'abcderabed' >>> str1.find("b") ## 查找字符串中的指定字符, 并返回索引 1 >>> str1.find("d") 3 >>> str1.find("x") ## 当字符串中不存在该字符串时, 返回-1 -1 >>> str1.find("b", 3) ## 可以指定查找的范围 7
014、index 返回字符串中指定字符的索引
>>> str1 = "abcderabed" >>> str1 'abcderabed' >>> str1.index("c") ## 返回匹配的索引 2 >>> str1.index("a", 3) ## 可以指定查找的范围 6 >>> str1.index("x") ## 当字符串中不存在该字符时, 返回异常 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
015、isalpha 判断字符串是否全部以字母组成
>>> str1 = "abcd" >>> str1 'abcd' >>> str2 = "abc123" >>> str2 'abc123' >>> str3 = "abc>>_" >>> str3 'abc>>_' >>> str1.isalpha() ## 判断字符串是否全部以字母组成 True >>> str2.isalpha() False >>> str3.isalpha() False
016、isdecimal 判断字符串是全部有数字组成
>>> str1 = "1234" >>> str1 '1234' >>> str2 = "12abc" >>> str2 '12abc' >>> str1.isdecimal() ## 判断字符串中字符是否全部由数字组成 True >>> str2.isdecimal() False
017、isdigit 判断字符串是否全部有数字组成
>>> str1 = "1234" >>> str1 '1234' >>> str2 = "12abc" >>> str2 '12abc' >>> str1.isdigit() ## 判断字符串是否全部有数字组成 True >>> str2.isdigit() False
018、isalnum判断字符串是否仅有字母和数字组成
>>> str1 = "1234" >>> str2 = "abcd" >>> str3 = "12abc" >>> str4 = "23__>>" >>> str1.isalnum() ## 判断字符串是否仅有数字和字母组成 True >>> str2.isalnum() True >>> str3.isalnum() True >>> str4.isalnum() False
019、isnumber判断字符串是否仅有数值构成
>>> str1 = "1234" >>> str2 = "1234abcd" >>> str3 = "1234>>" >>> str1.isnumeric() ## 判断字符串是否仅有数值组成 True >>> str2.isnumeric() False >>> str3.isnumeric() False
020、 isspace 判断字符串是否仅有空白字符组成
>>> str1 = " " >>> str2 = "abcd " >>> str1.isspace() ## 判断字符串是否仅有空白字符组成 True >>> str2.isspace() False
021、join用于将可迭代对象生成字符串
>>> test1 = ["a", "b", "c", "d"] >>> test2 = ("1","2","3","4") >>> "_".join(test1) ## join 将可迭代对象转换为字符串 'a_b_c_d' >>> "_".join(test2) '1_2_3_4'
022、title 将第一个字母转换为大写字母其余转换为小写字符(同capitalize)
>>> test1 = "aBc mNu xYZ" >>> test1 'aBc mNu xYZ' >>> test1.title() ## 将字符串中的单词首字符转换为大写 'Abc Mnu Xyz' >>> test1.capitalize() ## capitalize仅对一个字符转换为大写 'Abc mnu xyz'
023、strip 删除字符串两边的所有空白或者指定字符
>>> test1 = " abc " >>> test1.strip() ## 删除字符串两边的空白 'abc' >>> test2 = "xxyyyzzzx" ## 指定删除的字符 >>> test2.strip("x") 'yyyzzz'
024、rstrip 删除右边的空白或者指定的字符
>>> str1 = " abcd " >>> str1.rstrip() ## 删除字符串右边的空白 ' abcd' >>> str2 = "xxabcdxxx" ## 删除字符串右边的x >>> str2.rstrip("x") 'xxabcd'
025、lstrp删除左边的空白或指定字符
>>> str1 = " abcd " >>> str1.lstrip() ## 删除字符串左边的空白 'abcd ' >>> str2 = "xxxabcdxxxx" ## 删除字符串左边的x >>> str2.lstrip("x") 'abcdxxxx'
026、replace 将字符串中的字符替换为指定的字符
>>> str1 = "abcdabcdabcd" >>> str1.replace("a", "W") ## 将字符串中a替换为W 'WbcdWbcdWbcd' >>> str1.replace("a", "W", 1) ## 可以指定替换的次数 'Wbcdabcdabcd'
027、ljust 调整字符串的宽度,左对齐
>>> str1 = "abcd" >>> str1.ljust(10) ## 宽度为10, 左对齐 'abcd ' >>> str1.ljust(10, "*") ## 宽度为10, 左对齐, 指定填充字符 'abcd******'
028、rjust调整字符串的宽度, 右对齐
>>> str1 = "abcd" >>> str1.rjust(10) ## 指定字符串的宽度, 右对齐 ' abcd' >>> str1.rjust(10,">") ## 指定宽度, 指定填充字符 '>>>>>>abcd'
029、partition 以指定字符分割字符串, 返回三元组
>>> str1 = "abcdabcdabcdabcd" >>> str1.partition("c") ## 以指定字符分割字符串, 返回三元组 ('ab', 'c', 'dabcdabcdabcd') >>> str1.partition("abcd") ('', 'abcd', 'abcdabcdabcd') >>> str1.partition("xxxx") ## 如分割字符不存在,则三元组首个元素为整个字符串,后两个为空 ('abcdabcdabcdabcd', '', '')
030、rfind /rindex从字符串右侧查找字符串
>>> str1 = "abcdabcd" >>> str1.rfind("a") ## 从右侧查找字符串,返回索引 4 >>> str1.rfind("x") ## 如果不存在,返回-1 -1 >>> str1 = "abcdabcd" >>> str1.rindex("a") #3 从右侧查找, 返回索引 4 >>> str1.rindex("x") ## 如果不存在, 返回索引 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
031、rpartition 从右侧分割字符串, 返回三元组
>>> str1 = "abcdabcdabcd" >>> str1.rpartition("a") ## 从右侧以指定字符分割字符串, 返回三元组 ('abcdabcd', 'a', 'bcd') >>> str1.rpartition("xxxx") ('', '', 'abcdabcdabcd') ## 如果指定分隔符不存在, 则作为三元组的末尾元素, 其余为空
032、zfill 右对齐,调整字符串的宽度,默认以0填充
>>> str1 = "abcd" >>> str1.zfill(10) ## 右对齐, 调整字符串的宽度, 默认以0填充 '000000abcd'
033、expandtabs 将字符串中的制表符转换为空格
>>> str1 = "ab\tcd\txy" >>> str1.expandtabs() ## 将字符串中制表符转换为空格代替 'ab cd xy'
034、splitlines 以换行符分割字符串
>>> str1 = "ab\ncdxxyy\nzz\nmm\nqq" >>> str1 'ab\ncdxxyy\nzz\nmm\nqq' >>> str1.splitlines() ## 以换行符拆分字符串, 返回列表 ['ab', 'cdxxyy', 'zz', 'mm', 'qq']
035、translate 将字符转中的字符翻译成指定字符
>>> trantab = str.maketrans("abcd", "9713") ## 指定匹配规则, a替换为9、b替换为7以此类推 >>> str = "aakkbbcccd" >>> str.translate(trantab) ## 转换 '99kk771113'
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律