• Python String模块


    ​​

    活动地址:CSDN21天学习挑战赛

    字符串常量

    string.ascii_lowercase

    返回所有的小写字母。

    >>> string.ascii_lowercase
    'abcdefghijklmnopqrstuvwxyz'
    
    • 1
    • 2

    string.ascii_uppercase

    返回所有的大写字母。

    >>> string.ascii_uppercase
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    • 1
    • 2

    string.ascii_letters

    是上述 ascii_lowercaseascii_uppercase 常量的拼接。

    >>> string.ascii_letters
    'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    
    • 1
    • 2

    string.digits

    返回所有的十进制数字。

    >>> string.digits
    '0123456789'
    
    • 1
    • 2

    string.hexdigits

    返回所有的十六进制数字。

    >>> string.hexdigits
    '0123456789abcdefABCDEF'
    
    • 1
    • 2

    string.octdigits

    返回字符串 ‘01234567’,也即是八进制数字。

    >>> string.octdigits
    '01234567'
    
    • 1
    • 2

    string.punctuation

    返回由在 C 语言区域中被视为标点符号ASCII 字符组成的字符串。

    >>> string.punctuation
    '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    
    • 1
    • 2

    string.whitespace

    由被视为空白符号的 ASCII 字符组成的字符串。 其中包括空格制表换行回车进制纵向制表符

    >>> string.whitespace
    ' \t\n\r\x0b\x0c'
    
    • 1
    • 2

    string.printable

    返回由被视为可打印符号的 ASCII 字符组成的字符串。 这是digits, ascii_letters, punctuationwhitespace 的总和。

    >>> string.printable
    '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
    
    • 1
    • 2

    常用方法

    大小写转换

    str.capitalize()

    将字符串首字母大写。

    >>> "hello world".capitalize()
    'Hello world'
    
    • 1
    • 2

    str.title()

    str标题化(将所有单词变为首字母大写,其余字母小写)。

    >>> "hello world".title()
    'Hello World'
    
    • 1
    • 2

    注意和capitalize()函数做区分,capitalize()函数只对一个字符串的第一个单词的首字母大写,而title()函数是对每个单词都首字母大写。

    str.lower()

    将字符串str中所有大写字符变为小写。

    >>> "Hello WorldDDDDD".lower()
    'hello worldddddd'
    
    • 1
    • 2

    str.upper()

    将字符串str中所有小写字符变为大写。

    >>> "Hello WorldDDDDD".upper()
    'HELLO WORLDDDDDD'
    
    • 1
    • 2

    空格填充字符串

    str.center(width)

    将原字符串用空格填充成一个长度为width的字符串,原字符串内容居中。

    >>> "hello world".center(50)
    '                   hello world                    '
    
    • 1
    • 2

    这个在美化输出内容时比较有用。

    str.ljust(width)

    返回一个原字符串左对齐并使用空格填充至长度width的新的字符串。

    >>> "1,2,3,4,5".ljust(30)
    '1,2,3,4,5                     '
    
    • 1
    • 2

    str.rjust(width)

    返回一个原字符串右对齐并使用空格填充至长度width的新的字符串。

    >>> "1,2,3,4,5".rjust(30)
    '                     1,2,3,4,5'
    
    • 1
    • 2

    str.zfill(width)

    返回长度为width的字符串,原字符串右对齐,前面填充0。
    例子:

    >>> "   nihao".zfill(20)
    '000000000000   nihao'
    
    • 1
    • 2

    字符串编码解码

    str.encode(encoding='UTF-8', errors='stricts')

    以指定编码格式编码字符串。

    >>> "hello world".encode(encoding="utf-8")
    b'hello world'
    
    • 1
    • 2

    str.decode(encoding='UTF-8', errors='strict')

    以指定编码格式解码字符串。

    >>> a = "hello world".encode(encoding="gbk")
    >>> a.decode(encoding='gbk')
    'hello world'
    
    • 1
    • 2
    • 3

    查找字符串中某个字符的索引

    str.find(s)

    返回字符串s在字符串str中的位置索引,没有则返回-1。

    >>> "hello world".find('l')
    2
    
    • 1
    • 2

    注意:该函数从左往右搜索,只会返回第一个找到的索引。

    str.index(s)

    同上,但是如果s不存在于str中会抛出异常。

    >>> "hello world".index('l')
    2
    
    • 1
    • 2

    str.rfind(s)

    从右边开始查找,与find()函数类似。

    >>> "hello world".rfind('l')
    9
    
    • 1
    • 2

    str.rindex(s)

    从右边开始查找,与 index()函数类似。

    >>> "hello world".rindex('l')
    9
    
    • 1
    • 2

    字符串条件判断

    str.startswith(s)

    检查字符串str是否是以s开头,是则返回True,否则返回Flase

    >>> "   nihao".startswith(" ")
    True
    
    • 1
    • 2

    str.endswith(s)

    判断字符串str是否以字符串s结尾。

    >>> "hello world".endswith('d')
    True
    >>> "hello world".endswith('s')
    False
    
    • 1
    • 2
    • 3
    • 4

    str.isalnum()

    如果str至少有一个字符并且都是字母或者数字则返回True,否则返回False

    >>> "hello world".isalnum()
    False
    >>> "helloworld".isalnum()
    True
    >>> "".isalnum()
    False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    str.isalpha()

    如果str至少有一个字符并且都是字母则返回True,否则返回False

    >>> "helloworld".isalpha()
    True
    >>> "hello world".isalpha()
    False
    >>> "helloworld22".isalpha()
    False
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    str.isdigit()

    如果str只包含数字则返回True,否则返回False

    >>> "hello world".isdigit()
    False
    >>> "237507590257".isdigit()
    True
    
    • 1
    • 2
    • 3
    • 4

    str.islower()

    如果str存在区分大小写的字符,并且都是小写则返回True,否则返回False

    >>> "hello world".islower()
    True
    >>> "hello worlD".islower()
    False
    
    • 1
    • 2
    • 3
    • 4

    str.isupper()

    如果str存在区分大小写的字符,并且都是大写则返回True,否则返回False

    >>> "hello World".isupper()
    False
    >>> "GGGGAGDGDG".isupper()
    True
    
    • 1
    • 2
    • 3
    • 4

    str.isspace()

    如果str中只包含空格,则返回True,否则返回Flase

    >>> "hello World".isspace()
    False
    >>> "hello World   ".isspace()
    False
    >>> "   ".isspace()
    True
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    str.istitle()

    如果str是标题化的(单词首字母大写)则返回True,否则返回Flase

    >>> "Hello World".istitle()
    True
    >>> "Hello world".istitle()
    False
    
    • 1
    • 2
    • 3
    • 4

    去掉字符串不可见字符

    str.lstrip()

    去掉str左边不可见字符。

    >>> "  hello world    ".lstrip()
    'hello world    '
    
    • 1
    • 2

    str.rstrip()

    去掉str右边不可见字符。

    >>> "  hello world    ".rstrip()
    '  hello world'
    
    • 1
    • 2

    str.strip()

    去掉左右两边不可见字符。

    >>> "  hello world    ".strip()
    'hello world'
    
    • 1
    • 2

    字符串切片

    str.partition(s)

    s将字符串str分成三个值

    >>> "1,2,3,4,5".partition(",")
    ('1', ',', '2,3,4,5')
    >>> "1,2,3,4,5".partition("2")
    ('1,', '2', ',3,4,5')
    
    • 1
    • 2
    • 3
    • 4

    str.rpartition(s)

    从右边开始,用s将字符串str分成三个值,与partition()类似。

    >>> "1,2,3,4,5".rpartition(",")
    ('1,2,3,4', ',', '5')
    
    • 1
    • 2

    str.split(s)

    s为分隔符切片str

    >>> "1,2,3,4,5".split(",")
    ['1', '2', '3', '4', '5']
    
    • 1
    • 2

    str.splitlines()

    按照行分隔,返回一个包含各行作为元素的列表。例子:(识别\n为换行符)

    >>> "1,2,3,4,5\n2,3,4,5".splitlines()
    ['1,2,3,4,5', '2,3,4,5']
    >>> "1,2,3,4,5\n2,3,4,5 \n3 4\n\n5".splitlines()
    ['1,2,3,4,5', '2,3,4,5 ', '3 4', '', '5']
    
    • 1
    • 2
    • 3
    • 4

    替换字符串中的字符

    str.replace(a,b)

    将字符串str中的a替换成b

    >>> "hello world".replace('l', '2')
    'he22o wor2d'
    
    • 1
    • 2

    统计某字符出现的次数

    str.counts(s)

    返回字符串sstr中出现的次数。

    >>> "hello world".count("l")
    3
    
    • 1
    • 2
  • 相关阅读:
    Vue中组件的递归
    阶段五-Day03-Ajax
    UE4 源码阅读:从引擎启动到Receive Begin Play
    精神分裂型患者大脑结构和功能连接的改变
    【云原生 | Kubernetes 系列】--Gitops持续交付 CD Push Pipeline实现
    Vue3 企业级优雅实战 - 组件库框架 - 5 组件库通用工具包
    【码银送书第九期】《ChatGPT 驱动软件开发:AI 在软件研发全流程中的革新与实践》
    一种面向后端的微服务低代码平台架构设计
    性能优化之html、css、js三者的加载顺序
    V4L2 驱动架构介绍
  • 原文地址:https://blog.csdn.net/qq_37085158/article/details/126315631