009 字符串的内置方法 一

查看

  • dir(str): 列出 str 的方法
  • help(str): 查看开发者对 str 方法所编写的详细描述文档
    • help(str.capitalize) 可以仅查看 capitalize() 的用法

说明

  • capitalizezfill,Python3.8 有 45 个字符串方法
  • 一次写完有点伤身体,我将其分为三篇

capitalize()

  • 释义:使字符串首字母大写,其余小写,原本满足条件的就保持原样
>>> "YorkFish".capitalize()
'Yorkfish'
>>> "yorkFish".capitalize()
'Yorkfish'
>>> "Yorkfish".capitalize()
'Yorkfish'
>>> "yorkfish".capitalize()
'Yorkfish'
>>> "123yorkfish".capitalize()
'123yorkfish'
>>> "123YorkFish".capitalize()
'123yorkfish'
>>> 

casefold()

  • 释义:将整个字符串的字符改为小写
>>> "YorkFish".casefold()
'yorkfish'
>>> 

center(width)

  • 释义
    • width 为自定义的一段长度(通常要比字符串长)
    • 让字符串在这段长度里居中,其余位置用空格填充
>>> "YorkFish".center(30)
'           YorkFish           '
>>> 

count(sub[, start[, end]])

  • 释义
    • [] 里的参数可选
    • sub: 要计数的字符(查了一下词典,sub 有替身之意)
    • start: 开始位置,默认为开头
    • end: 结束位置,默认为结尾
>>> string = "Actions speak louder than words."
>>> string.count('A')
1
>>> string.count('A', 1)
0
>>> string.count('a', 8, 12)
1
>>> 

endswith(sub[, start[, end]])

  • 释义
    • 检查字符串是否以 sub 子字符串结束
    • 返回 True or False
    • 可用 startend 自定义范围
>>> string = "Winter has come, can spring be far behind?"
>>> string.endswith('?')
True
>>> string.endswith("far behind?")
True
>>> string.endswith("behind")
False
>>> string.endswith("come", 11, 15)
True
>>> string.endswith("e", 11, 15)
True
>>> 
  • startswith(prefix[, start[, end]]) 见第三弹

find(sub[, start[, end]])

  • 释义
    • 寻找字符串中是否包含 sub 子字符串
    • 若有,返回索引值;若无,返回 -1
>>> string = "When I was a child, happiness is a very simple thing."
>>> string.find("is")
30
>>> string.find("is", 40)
-1
>>> string.find("simple", 30, 50)
40
>>> 
  • rfind(sub[, start[, end]]) 用法类似,从右边开始查找

index(sub[, start[, end]])

  • 释义:跟 find() 的用法仅一点不同:若索引不到,会抛异常
>>> string = "When I grow up, simplicity is a very happy thing!"
>>> string.index("happy")
37
>>> string.index("simple")
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    string.index("simple")
ValueError: substring not found
>>> 
  • rindex(sub[, start[, end]]) 用法类似,从右边开始查找

isalnum()

  • 释义
    • 检查字符串中所有的字符是否都是字母或数字
    • 返回 True or False
>>> "YorkFish2019".isalnum()
True
>>> "YorkFish 2019".isalnum()
False
>>> ''.isalnum()
False
>>> "YorkFish②⓪①⑨".isalnum()
True
>>> 
  • Python 认为带圈的 0-9 也属于数字

isalpha()

  • 释义
    • 检查字符串中所有的字符是否都是字母
    • 返回 True or False
>>> "YorkFish".isalpha()
True
>>> "YorkFish2019".isalpha()
False
>>> "鱼".isalpha()
True
>>> 
  • Python 认为汉字也是 alphabet

isascii()

  • 释义
    • 检查字符串中所有的字符是否都是 ASCII
    • 返回 True or False
>>> chr(0).isascii()
True
>>> chr(127).isascii()
True
>>> chr(128).isascii()
False
>>> ''.isascii()
True
>>> 

isdecimal()

  • 释义
    • 检查字符串中所有的字符是否都只包含十进制数字
    • 返回 True or False
>>> "YorkFish2019".isdecimal()
False
>>> "2019".isdecimal()
True
>>> "0xfe".isdecimal()
False
>>> "②".isdecimal()
False
>>> 

isdigit()

  • 释义
    • 检查字符串中所有的字符是否都是数字
    • 返回 True or False
>>> "YorkFish2019".isdigit()
False
>>> "2019".isdigit()
True
>>> "②".isdigit()
True
>>> 
原文地址:https://www.cnblogs.com/yorkyu/p/10262512.html