码迷,mamicode.com
首页 > 编程语言 > 详细

python之路--str类型

时间:2018-02-12 00:48:51      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:git   组成   有一个   *args   大小写   table   查找   upper   enc   

str类型内定义的函数一些函数

  • capitalize:首字母变大写,其余小写

    函数原型:def capitalize(self)

    用法:

    str1.capitalize()

  • casefold:全部变小写

    函数原型:def casefold(self)

    用法:

    str1.casefold()

  • center:内容居中

    函数原型:def center(self, width, fillchar=None)

    用法:

    str1.center(n,fill) #n表示总长度,fill表示填充符号

  • count:统计子序列个数

    函数原型:def count(self, sub, start=None, end=None)

    用法:

    str1.count(sub,m,n) #在[m,n)区间中统计sub的个数

  • encode:编码,针对unicode

    函数原型:def encode(self, encoding=‘utf-8‘, errors=‘strict‘)

  • endswitch:判断是否以 suffix 结束,返回False or True

    函数原型:def endswith(self, suffix, start=None, end=None) 用法:

    str1.endswith("suffix",m,n) #在[m,n)区间查找是否以suffix结尾

  • startswith:判断是否以 suffix 开头,返回False or True

    函数原型: def startswith(self, prefix, start=None, end=None) 用法:

    str1.endswith("prefix",m,n) #在[m,n)区间查找是否以prefix开头

  • expandtabs:将tab转换成空格,默认一个tab转换成8个空格

    函数原型:def expandtabs(self, tabsize=8)

    用法:

    str1.expandtabs()

  • find:寻找子序列位置,如果没找到,返回 -1

    函数原型:def find(self, sub, start=None, end=None)

    用法:

    str1.find(sub,m,n) #查找sub的位置并返回开始的值

  • format:字符串格式化,动态参数

    函数原型:def format(self, *args, **kwargs) 用法:

    str1="hello {0} , hello {1}"

    str1.format("world","python")

  • index:子序列位置,如果没找到,报错

    函数原型:def index(self, sub, start=None, end=None)

    用法:

    str1.index(sub,m,n)

  • isalnum:是否是字母和数字

    函数原型:def isalnum(self)

  • isalpha:是否是字母

    函数原型:def isalpha(self)

  • isdigit:是否是数字

    函数原型:def isdigit(self)

  • islower:是否小写

    函数原型:def islower(self)

  • isspace:检测字符串是否只由空格组成。

    函数原型:def isspace(self)

  • istitle:测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。

    函数原型:def istitle(self)

  • isupper:判断是否为大写

    函数原型:def isupper(self)

  • join:连接

    函数原型:def join(self, iterable)

    用法:

    "fill".join(str1) #用fill符号连接str1之间的字符

    例如:

    str1="12345"

    "+".join(str1)

    输入为1+2+3+4+5

  • ljust:内容左对齐,右侧用fillchar填充

    函数原型:def ljust(self, width, fillchar=None)

    用法:

    str1.ljust(m,fill)#m表示长度,fill表示填充的字符

  • lower:变小写

    函数原型:def lower(self)

    用法:

    str1.lower()

  • upper:变大写

    函数原型:def upper(self)

    用法:

    str1.upper()

  • strip:移除两边的chars,默认为空格

    函数原型:def strip(self, chars=None)

    用法:

    str1.strip("chars")#chars表示要移除的字符串

  • partition:分割成前中后三部分(从左向右),有一个rpartition从右向左

    函数原型:def partition(self, sep)

    用法: str1.partition(sep) # sep变成一部分,sep前后各变成一部分

  • replace:替换

    函数原型:def replace(self, old, new, count=None)

    用法: str1.replace(sub1,sub2,n) #将sub1替换为sub2,替换n次,默认替换所有

  • split:分割, maxsplit最多分割几次

    函数原型:def split(self, sep=None, maxsplit=-1)

    用法:

    str1.split(sep,m) # 根据sep分割m次,没有m的话,有sep就分割

  • splitlines:根据按照行(‘\r‘, ‘\r\n‘, \n‘)分隔,返回一个包含各行作为元素的列表,如果参数 keepends为False,不包含换行符,如果为 True,则保留换行符。

    函数原型:def splitlines(self, keepends=None)

    用法: str1.splitlines()

  • swapcase:大小写互换

    函数原型:def swapcase(self)

  • title:将字符串变为单词开头为大写,其他为小写

    函数原型:def title(self)

  • translate:转换,需要先做一个对应表,最后一个表示删除字符集合

    函数原型:def translate(self, table)

    用法:

    str1.translate(trantab),不过需要先使用maketrans

    例如:

    intab = "aeiou"
    outtab = "12345"
    trantab = temp.maketrans(intab, outtab)
    str = "this is string example....wow!!!"
    print(str.translate(trantab))
    #输出结果th3s 3s str3ng 2x1mpl2....w4w!!!

python之路--str类型

标签:git   组成   有一个   *args   大小写   table   查找   upper   enc   

原文地址:https://www.cnblogs.com/chaguang/p/8443527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!