• Python 字符串内置方法笔记


      

    一. 大小写转换

    ① capitalize()

    capitalize() #字符串首字母大写

    >>> str0 = 'hello World'
    >>> str0.capitalize()
    'Hello world'
    

    ②upper(), lower()

    upper() #将字符串的所有小写字符转化为大写
    lower() #将字符串的所有大写字符转化为小写

    >>> str1 = 'Hello World'
    >>> str1.upper()
    'HELLO WORLD'
    >>> str1.lower()
    'hello world'
    

    ③swapcase()

    swapcase() #将字符串中的大写字符改为小写,小写字符改为大写

    >>> str1 = 'Hello World'
    >>> str1.swapcase()
    'hELLO wORLD'
    

    二. 内容判断

    ①startswith(),endswith()

    startswith() #判断字符串是否以指定参数开始,返回True或False
    endswith() #判断字符串是否以指定参数结束,返回True或False

    >>> str1 = 'Hello World'
    >>> str1.startswith('h')
    False
    >>> str1.endswith('d')
    True
    

    tip:判断时大小写是要区分的,可以添加第二、三个参数来限定范围。

    ②isupper(),islower()

    isupper() #如果字符串中的字母均为大写返回True,否则返回 False
    islower() #如果字符串中的字母均为小写返回True,否则返回 False

    >>> str1 = 'Hello World'
    >>> str2 = '123asdxfgs...'
    >>> str1.isupper()
    False
    >>> str2.islower()
    True
    

    tip:字符串中可以含数字和特殊字符

    ③isalpha(),isdigit(),isalnum()

    isalpha() #如果字符串中只包含字母返回 True,否则返回 False
    isdigit() #如果字符串中只包含数字返回 True,否则返回 False
    isalnum() #如果字符串中只包含字母或数字返回 True,否则返回 False

    >>> str3 = '123456789'
    >>> str2 = '123asdxfgs...'
    >>> str3.isalpha()
    False
    >>> str3.isdigit()
    True
    >>> str2.isalnum()
    False
    

    三. 内容查找

    find(),rfind(),index(),rindex()

    find() #查找指定参数是否在字符串中,查找成功返回字符下标,失败则返回-1
    rfind() #功能和find()一致,但查找方向从右边开始
    index() #和find功能一样,但查找失败会产生错误
    rindex() #功能和index()一致,但查找方向从右边开始

    >>> str2 = '123asdxfgs...'
    >>> str2.find('123')
    0
    >>> str2.find('1234')
    -1
    >>> str2.index('x')
    6
    >>> str2.index('b')
    Traceback (most recent call last):
      File "<pyshell#33>", line 1, in <module>
        str2.index('b')
    ValueError: substring not found
    >>> str4 = 'test88ooo88'
    >>> str4.find('88')
    4
    >>> str4.rfind('88')
    9
    

    tip:可以加上第二、三个参数来限制范围。

    四. 替换, 添加, 删除

    ①strip(),lstrip(),rstrip()

    strip() #默认删除字符串前后的空格,若加上参数,则改为删除字符串前后的该参数
    lstrip() #去掉字符串前面的空格,若加上参数,则改为删除字符串前的该参数
    rstrip() #删除字符串末尾的空格,若加上参数,则改为删除字符串后的该参数

    >>> str5 = '   content    '
    >>> str5.lstrip()
    'content    '
    >>> str5.rstrip()
    '   content'
    >>> str5.strip()
    'content'
    >>> str5.strip('t')
    '   content    '
    >>> str6 = '123and123'
    >>> str6.strip('123')
    'and'
    >>> str6.lstrip('123')
    'and123'
    

    tip:strip()只能删除字符串前后的指定内容,不能误解为删除该字符串中所有的该字符,若想实现此功能,建议使用replace()

    ②replace()

    replace(old,new[,count]) #将字符串中的所有old用new替换,若使用count参数,则替换次数不超过count次。

    >>> str7 = 'xxHxexlxxlxo Wxxxxorld'
    >>> str7.replace('x', '')
    'Hello World'
    >>> str7.replace('x', 'A')
    'AAHAeAlAAlAo WAAAAorld'
    >>> str7.replace('x', 'A', 1)
    'AxHxexlxxlxo Wxxxxorld'
    

    ③format()

    format() #格式化字符串

    >>> "姓名:{}, 性别:{}".format('张三','男')
    '姓名:张三, 性别:男'
    >>> "姓名:{a}, 性别:{b}".format(b='男',a='张三')
    '姓名:张三, 性别:男'
    

    tip:常与{}一起使用,参数个数由自己决定。

    ④join()

    join() #以原字符串作为分隔符,插入到参数中每个字符之间。

    >>> str_break = 'xx'
    >>> str_break.join('AB')
    'AxxB'
    >>> str_break.join('ABC')
    'AxxBxxC'
    

    五. 分割

    ①split(), rsplit()

    split() #以第一个参数为分隔符分割字符串,返回一个列表
    rsplit() #和split()功能相同,但是从右边开始分割

    >>> str8 = 'AxxBxxC'
    >>> str8.split('xx')
    ['A', 'B', 'C']
    >>> str8.split('x')
    ['A', '', 'B', '', 'C']
    >>> str8.split('xxx')
    ['AxxBxxC']
    

    tip:可以加上第二个参数限制次数。

    >>> str8.split('xx', 1)
    ['A', 'BxxC']
    

    ②partition(),rpartition()

    partition() #将字符串从参数处分成'参数前','参数','参数后'三段,返回一个三元元组
    rpartition() #功能和partition()一致,但从右边开始

    >>> str8 = 'AxxBxxC'
    >>> str8.partition('xx')
    ('A', 'xx', 'BxxC')
    >>> str8.rpartition('xx')
    ('AxxB', 'xx', 'C')
    >>> str8.partition('zzz')
    ('AxxBxxC', '', '')
    

    tip:若找不到参数,则返回('字符串', '', '')

    六. 计数

    count()

    count() #参数为字符串的一个子串,返回该子串出现的次数

    >>> str8 = 'AxxBxxC'
    >>> str8.count('xx')
    2
    

    tip:可以加上第二,三个参数来限定范围。

    七、其他

    center()

    center() #将某字符串居中,并以指定字符填充至指定长度。

    >>> "test".center(20, '*')
    '********test********'
  • 相关阅读:
    博客园美化-SimpleMemor
    Java多线程-synchronized与ReentrantLock
    springboot中删除@SessionAttributes注解的属性
    SSM整合笔记
    Spring中xml和注解方式使用AOP
    Mysql 数据库基本操作
    Mysql 二进制包安装
    named piped tcp proxy 下载
    docker容器中日志文件过大处理方法
    自动做bond的脚本
  • 原文地址:https://www.cnblogs.com/68xi/p/11621959.html
Copyright © 2020-2023  润新知