python之路--str類型

來源:互聯網
上載者:User

標籤: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類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.