Python str对象方法

你的名字 2022-06-15 08:11 300阅读 0赞

内建类型之文本序列str

本文只讲str对象的方法

重要声明:字符串str对象为unicode不可变序列,对它的任何操作不会改变它本身
  • 1 str.capitalize()

    1. 字符串首字母大写
    2. s = 'china is a great country'
    3. s.capitalize() ---> 'China is a great country'
  • 2 str.casefold()

    1. 德国����人用的上
  • 3 str.center(width[, fillchar ])

    1. 字符串居中
    2. width : 字符串宽度
    3. fillchar: 填充字符,可省略
    4. s = 'china is a great country'
    5. s.center(30,'*')-->'***china is a great country***'
  • 4 str.count(sub[, start[, end ] ])

    1. 从字符串strstart下标位置与end下标位置之间查找子字符串sub
    2. s = 'she is a girl she is charming...'
    3. s.count('is')----> 2
  • 5 str.encode(encoding=”utf-8”, errors=”strict”)

    1. 字符串编码,返回字节串, 默认使用utf-8 字符集,一个汉字占用三个字节
    2. s = 'hello 中国'
    3. s.encode() ---> b'hello \xe4\xb8\xad\xe5\x9b\xbd'
  • 6 str.endswith(suffix[, start[, end ] ])

    1. 检测字符串str 是否含有suffix后缀
    2. s = 'hello 中国'
    3. s.endswith('中国') --> True
  • 7 str.expandtabs(tabsize=8)

    1. 将字符串中出现的制表符\t替换为空格
  • 8 str.find(sub[, start[, end ] ])

    1. 从字符串strstart位置开始到end位置结束,查找子串sub,返回子串sub出现的位置
    2. start:缺省值为字符串起点
    3. end: 缺省值为字符串长度
    4. s = 'hello 中国'
    5. s.find('lo') ----> 3
    6. 没找到 返回-1
  • 9 str.format(*args, **kwargs)

    1. 字符串格式化,可以向字符串内插入内容
    2. '{}对郭靖说,帅哥,来嘛,{}缺一'.format('黄蓉','三')
    3. '黄蓉对郭靖说,帅哥,来嘛,三缺一'
    4. 注意字符串中的{}
    5. '{0}二{0},向右转'.format('一')
    6. '一二一,向右转'
    7. {}中带数字
    8. '{name}的家在{address}'.format(name='李敖',address='台湾')
    9. '李敖的家在台湾'
    10. 还有更好玩的自己查手册吧~
    11. 这个方法很重要~我猜的~
  • 10 str.format_map(mapping)

    1. 跟上面��的类似
    2. '{name} was born in {country}'.format_map(Default(name='Guido'))
    3. 'Guido was born in country'
    4. 规则:给出名称的值则使用值,否则直接使用名称本身
  • 11 str.index(sub[, start[, end ] ])

    1. 这个方法与str.find 方法功能相同,但是如果没找子串到会报错
  • 12 str.isalnum()

    1. 检测字符串中每个字符是否是字母或者数字
    2. 字母: a-z 不区分大小写
    3. 阿拉伯数字:0-9
    4. 汉字数字: 二三四
    5. 汉字数字大写:壹贰叁 。。。
    6. 罗马数字和英文字母重合了~
    7. 'hello21312HR'.isalnum() --> True
    8. '一'.isalnum() --> True
    9. '肆'.isalnum()---> True
  • 12 str.isalpha()

    1. 检测字符串中每个字符是否是unicode字符
    2. '美女'.isalpha() -->True
  • 13 str.isdecimal()

    1. 检测字符串中每个字符是否是十进制字符
    2. '10'.isdecimal()----> True
  • 14 str.isdigit()

    1. 阿拉伯数字
    2. '123'.isdigit()--->True
    3. 有点晕~
  • 15 str.isidentifier()

    1. 根据国家要求这个方法的使用我不能公布
  • 16 str.islower()

    1. 检测字符串中的字符是否都是小写字母
    2. 'hello'.islower() ---> True
  • 17 str.isnumeric()

    1. 检测字符串中的字符是否都是数字字符
    2. '123一'.isnumeric()--->True
  • 18 str.isspace()

    1. 是否是空白字符
    2. \t \v \f \b \r
  • 19 str.istitle()

    1. 检测字符串中每个单词是否首字母大写
    2. 'Hello World'.istitle() --->True
    3. 'Hello world'.istitle() --->False
  • 20 str.isupper()

    1. 检测字符串中的字母是否都是大写字母
    2. GOOD GIRL‘.isupper()--->True
    3. 注意:字符串中可出现空白、标点符号等
  • 21 str.join(iterable)

    1. 将序列iterable对象中的每个元素使用str粘到一起,产生新的字符串
    2. '**'.join(['1','2','3']) ---> '1**2**3'
    3. 注意:iterable中的元素需要是字符串对象
  • 22 str.ljust(width[, fillchar ])

    1. 左对齐
    2. 'beautiful girl'.ljust(30,'<')
    3. 'beautiful girl<<<<<<<<<<<<<<<<'
  • 23 str.lower()

    1. 字符全部转小写
    2. '中国 GOOD'.lower() --->'中国 good'
  • 24 str.lstrip([chars ])

    1. 从左边开始修剪字符串
    2. chars :缺省时表示剔除左边出现的空白字符
    3. '\t\n hello'.lstrip() ---> 'hello'
    4. 'www.example.com'.lstrip('w.e') --->'xample.com'
    5. 规则:从str左边开始,将出现在chars中的字符剔除,直到碰到没出现的字符时结束
  • 25 str.maketrans(x[, y[, z ] ])
  • 26 str.partition(sep)

    1. 字符串分段
    2. 将字符串strsep分段,默认从左边开始搜索
    3. 若出现sep 返回三个元素的元组,[sep之前的部分,sep, sep之后的部分]
    4. 'there is a test'.partition('is') -->('there ', 'is', ' a test')
    5. 如果sep 出现在字符串的最开头或者结尾部分呢? 自己动手试试喽~
  • 27 str.replace(old, new[, count ])

    1. 将字符串str中的old子串使用new子串替换,替换次数count,默认全部替换
    2. 'there is a good girl'.replace('oo','o') ---->'there is a god girl'
  • 28 str.rfind(sub[, start[, end ] ])

    1. 从右侧开始查找子串
    2. 参考str.find
  • 29 str.rindex(sub[, start[, end ] ])

    1. 从右侧开始查找子串,获取下标
    2. 参考str.index
  • 30 str.rjust(width[, fillchar ])

    1. 右对齐
    2. 'beautiful girl'.rjust(30,'>')
    3. '>>>>>>>>>>>>>>>>beautiful girl'
  • 31 str.rpartition(sep)

    1. 字符串分段,从右侧开始
    2. 使用场景之一:获取文件名
    3. 'http://www.filepath/doc1/doc/fff/imgfile.png'.rpartition('/')
    4. ('http://www.filepath/doc1/doc/fff', '/', 'imgfile.png')
    5. 元组的最后一个元素即文件名
  • 32 str.rsplit(sep=None, maxsplit=-1)

    1. 字符串切分,从右侧开始
    2. 参考str.split
  • 33 str.rstrip([chars ])

    1. 字符串修剪,从右侧开始
    2. 参考str.strip
  • 34 str.split(sep=None, maxsplit=-1)

    1. 字符串切分,默认从左侧开始
    2. sep :切分字符串
    3. maxsplit:切分次数
    4. '1,2,3'.split(',')--->['1', '2', '3']
    5. '1,2,3'.split(',', maxsplit=1)--->['1', '2,3']
    6. '1,2,,3,'.split(',') --->['1', '2', '', '3', '']
    7. ' 1 2 3 '.split()---> ['1', '2', '3']
    8. 出现连续的切分符号
    9. 12’.split(’,’) -->[’1’, ’’, 2’]
  • 35 str.splitlines([keepends ])

    1. 将字符串以换行符切分
    2. 1 不保留换行符
    3. 'ab c\n\nde fg\rkl\r\n'.splitlines() -->['ab c', '', 'de fg', 'kl']
    4. 2 保留换行符
    5. 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
    6. ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
  • 36 str.startswith(prefix[, start[, end ] ])

    1. 检测字符串str是否含有前缀prefix
    2. 参考str.endswith
  • 37 str.strip([chars ])

    1. 从两边同时开始修剪字符串
    2. 'www.example.com'.strip('w.emo') ---> 'xample.c'
  • 38 str.swapcase()

    1. 大小写互转
    2. 'hello WORLD'.swapcase() --->'HELLO world'
  • 39 str.title()

    1. 单词首字母大写
    2. 'heLLo, woRld'.title()--->'Hello, World'
    3. 注意:
    4. "they're bill's friends from the UK".title()---->"They'Re Bill'S Friends From The Uk" 不合理
    5. 请使用正则re模块解决
  • 40 str.translate(table)

    1. 留着
  • 41 str.upper()

    1. 全部转大写
  • 42 str.zfill(width)

    1. 0 width 宽度
    2. 举个��:
    3. 'helle'.zfill(20) --->'000000000000000helle'
    4. '1000'.zfill(10) ---->'0000001000'
    5. '-1000'.zfill(10) --->'-000001000'

  • 参考:官方手册Library 版本3.5.3
  • str 提供的方法只能满足简单使用的场景,如果过于复杂的字符串处理请使用re模块
  • 初稿 version1.0 日期:2017.5.27
  • 作者 王召洲
  • 联系QQ:956594065
  • 别名:隔壁老王

发表评论

表情:
评论列表 (有 0 条评论,300人围观)

还没有评论,来说两句吧...

相关阅读