Python str对象方法

来源:互联网 发布:玩名堂换域名吗? 编辑:程序博客网 时间:2024/06/06 03:29

内建类型之文本序列str

本文只讲str对象的方法

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    留着
  • 41 str.upper()

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

    填0 width 宽度举个��:'helle'.zfill(20) --->'000000000000000helle''1000'.zfill(10) ---->'0000001000''-1000'.zfill(10) --->'-000001000'

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