Python str函数

函数太多,如果实在不记得,就使用 help(str.XXX)来查看一下就好了。

浏览大概知道有这么个方法,用的时候现查就好了。

python2和python3有很大的区别,具体使用时用help看函数定义。

string.capitalize():把字符串的第一个字符大写

string.center(width):写注释的时候,可以拿来用,方便。

string.count(str, beg=0,end=len(string)):代码测试过,搜索范围是[begin,end),begin,end实际上是下标,所以,如果想搜索整个字符串,需要end>=len(str) 与 切片时的范围选择是一致的。begin也可以写负数,-1表示最后一个,并且这个负数的取值范围更广,可以写>length的负数,也不用考虑begin是不是在end前,函数会自己考虑的。

string.endswith(obj, beg=0,end=len(string))

string.find(str, beg=0,end=len(string)):找到返回index,没找到返回-1

string.rfind(str, beg=0,end=len(string)):类似于find()函数,不过是从右边开始查找.

string.index(str, beg=0,end=len(string)) :跟find()方法一样,只不过如果str 不在string 中会报一个异常.

string.rindex( str, beg=0,end=len(string)) :类似于index(),不过是从右边开始.

string.isalnum():至少有一个字母或者数字,返回True

string.isalpha():至少有一个字符,并且所有的内容都是字符,返回True

string.isdecimal():只包含十进制数字,返回True

string.isdigit():只包含数字,返回True

string.islower():string中包含且至少包含一个可以区分大小写的字符,并且这个字符是小写字符(可区分大小写并且是小写),返回True

string.isupper(): string中包含且至少包含一个可以区分大小写的字符,并且串中的字符都是大写。返回True

string.lower() :将string字符串中包含的可以区分大小写的字符,转换为小写字符。不要求string中必须都是可区分大小写的字符,与string.islower区分

string.upper():将string字符串中包含的可以区分大小写的字符,转换为大写字符。不要求string中必须都是可区分大小写的字符,与string.isupper()区分

string.swapcase() 翻转string 中的大小写

string.isnumeric():只包含数字字符,返回True     注意与string.isdecimal区分。

string.isspace():只包含空格,返回True     这个应该在代码中经常会遇到,比如判断输入条件,用isspace比用 ‘ ‘ 靠谱都了

string.title():返回”标题化”的string,即所有单词都是以大写开始,其余字母均为小写(见istitle())

string.istitle():如果string是标题化的,返回True。 标题化:串中的所以单词的首字母大写  注意与string.Captical区分

string.join(seq) :’+’.join(‘world’) :运行结果 ‘w+o+r+l+d’:  注意是string来链接迭代器seq中的元素。seq必须为迭代器才行。否则报错

string.ljust(width):返回左对齐的字符串,并使长度变为width,不够就用空格填充。

string.rjust(width):返回左对齐的字符串,并使长度变为width,不够就用空格填充。

string.lstrip() :仅去掉左边的空格

string.rstrip(): 删除string 字符串右边的空格.

string.strip([obj]) :在string 上执行lstrip()和rstrip()

string.partition(str):结果为:(string_pre_str,str,string_post_str)元组,find和index的结合。如果没有找到str,则结果为(string_pre_str,”,”)

string.rpartition(str):从右边开始查找的partition函数

string.replace(str1, str2,num=string.count(str1)):可以指定最大的替换次数:num

string.split(sep=None, maxsplit=-1)):切片,最多寻找前num个分隔符来分割,即结果为长度为num+1的list

‘hello world hi cute’.split(‘ ‘)

[‘hello’, ‘world’, ‘hi’, ‘cute’]

‘hello world hi cute’.split(‘ ‘,2)

[‘hello’, ‘world’, ‘hi cute’]

string.splitlines(keepends):以\n来分割字符串,keepends设置是否保留\n

‘hello\nworld\ncute\n\n\n’.splitlines(True) 

[‘hello\n’, ‘world\n’, ‘cute\n’, ‘\n’, ‘\n’] 

‘hello\nworld\ncute\n\n\n’.splitlines(False) 

[‘hello’, ‘world’, ‘cute’, ”, ”]

string.startswith(obj, beg=0,end=len(string))

string.translate(str, del=””) 根据str 给出的表(包含256 个字符)转换string 的字符,要过滤掉的字符放到del 参数中

与生活中的翻译类似:将一种集合A映射为另一种集合B,有时候也需要将集合A中某个在集合B中没有映射关系的元素过滤掉,放在del中就可以了。

string.zfill(width):string右对齐,用zero填充至width的宽度。多用于数字的对齐格式,数字左边填充zero不影响数字本身,所以没有rzfill的函数与之对应

string.decode(encoding=’UTF-8′,errors=’strict’) 以encoding 指定的编码格式解码string,如果出错默认报一个ValueError 的异常, 除非errors 指定的是’ignore’ 或者’replace’

string.encode(encoding=’UTF-8′,errors=’strict’)a 以encoding 指定的编码格式编码string,如果出错默认报一个ValueError 的异常,除非errors 指定的是’ignore’或者’replace’

    原文作者:Vicky_Yang
    原文地址: https://www.jianshu.com/p/6082e3805184
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞