1. 程式人生 > >python之路--str類型

python之路--str類型

isspace lock encode 空格 nic emp upper ket pen

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類型