python Str類的方法介紹

來源:互聯網
上載者:User
python Str類的方法介紹

#capitalize():字串首字元大寫string = 'this is a string.'new_str = string.capitalize()print(new_str)#輸出:This is a string.  #center(width, fillchar=None):將字串放在中間,在指定長度下,首尾以指定輸入鍵台string = 'this is a string.'new_str = string.center(30,'*')print(new_str)#輸出:******this is a string.*******  #count(sub, start=None, end=None):計算字串中某字元的數量string = 'this is a string.'new_str = string.count('i')print(new_str)#輸出:3  #decode(encoding=None, errors=None):解碼string = 'this is a string.'new_str = string.decode()print(new_str)  #encode(self, encoding=None, errors=None):編碼string = 'this is a string.'new_str = string.encode()print(new_str)  #endswith(self, suffix, start=None, end=None):判斷是否以某字元結尾string = 'this is a string.'new_str = string.endswith('ing.')print(new_str)#輸出:Truenew_str = string.endswith('xx')print(new_str)#輸出:False  #expandtabs(self, tabsize=None):返回定位字元。tabsize此選項指定要定位點化“\t' 的字元數,預設為8string_expandtabs = 'this\tis\ta\tstring.'new_str = string_expandtabs.expandtabs()print(new_str)#輸出:this    is      a       string.  #find(self, sub, start=None, end=None):在字串中尋找指定字元的位置string = 'this is a string.'new_str = string.find('a')    #找的到的情況print(new_str)#輸出:8new_str = string.find('xx')    #找不到的情況返回-1print(new_str)#輸出:-1  #format(*args, **kwargs):類似%s的用法,它通過{}來實現string1 = 'My name is {0},my job is {1}.'new_str1 = string1.format('yue','tester')print(new_str1)#輸出:My name is yue,my job is tester.string2 = 'My name is {name},my job is {job}.'new_str2 = string2.format(name='yue',job='tester')print(new_str2)#輸出:My name is yue,my job is tester.  #index(self, sub, start=None, end=None):;類似findstring = 'this is a string.'new_str = string.index('a')    #找的到的情況print(new_str)#輸出:8new_str = string.index('xx')    #找不到的情況,程式報錯print(new_str)#輸出:程式運行報錯,ValueError: substring not found  #isalnum(self):判斷字串中是否都是數字和字母,如果是則返回True,否則返回Falsestring = 'My name is yue,my age is 18.'new_str = string.isalnum()print(new_str)#輸出:Falsestring = 'haha18121314lala'new_str = string.isalnum()print(new_str)#輸出:True  #isalpha(self):判斷字串中是否都是字母,如果是則返回True,否則返回Falsestring = 'abcdefg'new_str = string.isalpha()print(new_str)#輸出:Truestring = 'my name is yue'new_str = string.isalpha()    #字母中間帶空格、特殊字元都不行print(new_str)#輸出:False  # isdigit(self):判斷字串中是否都是數字,如果是則返回True,否則返回Falsestring = '1234567890'new_str = string.isdigit()print(new_str)#輸出:Truestring = 'haha123lala'new_str = string.isdigit()    #中間帶空格、特殊字元都不行print(new_str)#輸出:False  # islower(self):判斷字串中的字母是否都是小寫,如果是則返回True,否則返回Falsestring = 'my name is yue,my age is 18.'new_str = string.islower()print(new_str)#輸出:Truestring = 'My name is Yue,my age is 18.'new_str = string.islower()print(new_str)#輸出:False  # isspace(self):判斷字串中是否都是空格,如果是則返回True,否則返回Falsestring = '      'new_str = string.isspace()print(new_str)#輸出:Truestring = 'My name is Yue,my age is 18.'new_str = string.isspace()print(new_str)#輸出:False  # istitle(self):檢測字串中所有的單詞拼字首字母是否為大寫,且其他字母為小寫。string = 'My Name Is Yue.'new_str = string.istitle()print(new_str)#輸出:Truestring = 'My name is Yue,my age is 18.'new_str = string.istitle()print(new_str)#輸出:False  # isupper(self):檢測字串中所有的字母是否都為大寫。string = 'MY NAME IS YUE.'new_str = string.isupper()print(new_str)#輸出:Truestring = 'My name is Yue.'new_str = string.isupper()print(new_str)#輸出:False  # join(self, iterable):將序列中的元素以指定的字元串連產生一個新的字串。string = ("haha","lala","ohoh")str = "-"print(str.join(string))#輸出:haha-lala-ohoh  # ljust(self, width, fillchar=None):返回一個原字串靠左對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字串。string = "My name is yue."print(string.ljust(18))#輸出:My name is yue.  # lower(self):轉換字串中所有大寫字元為小寫。string = "My Name is YUE."print(string.lower())# 輸出:my name is yue.  # lstrip(self, chars=None):截掉字串左邊的空格或指定字元。string = "   My Name is YUE."print(string.lstrip())#輸出:My Name is YUE.string = "My Name is YUE."print(string.lstrip('My'))#輸出: Name is YUE.  # partition(self, sep):根據指定的分隔字元將字串進行分割。string = "http://www.mingyuanyun.com"print(string.partition('://'))#輸出:('http', '://', 'www.mingyuanyun.com')  #replace(self, old, new, count=None):把字串中的 old(舊字串) 替換成 new(新字串),如果指定第三個參數max,則替換不超過 max 次。string = "My name is yue."print(string.replace("yue","ying"))#輸出:My name is ying.  # rfind(self, sub, start=None, end=None):返回字串最後一次出現的位置,如果沒有匹配項則返回-1。string = "My name is yue."print(string.rfind('is'))#輸出:8string = "My name is yue."print(string.rfind('XXX'))#輸出:-1  # rindex(self, sub, start=None, end=None):返回子字串 str 在字串中最後出現的位置,如果沒有匹配的字串會報異常,你可以指定選擇性參數[beg:end]設定尋找的區間。string = "My name is yue."print(string.rindex('is'))#輸出:8string = "My name is yue."print(string.rindex('XXX'))#輸出:ValueError: substring not found  # rjust(self, width, fillchar=None):返回一個原字串靠右對齊,並使用空格填充至長度 width 的新字串。如果指定的長度小於字串的長度則返回原字串。string = "My name is yue."print(string.rjust(18))#輸出:   My name is yue.  # rpartition(self, sep):根據指定的分隔字元將字串從右進行分割。string = "http://www.mingyuanyun.com"print(string.rpartition('.'))#輸出:('http://www.mingyuanyun', '.', 'com')  # split(self, sep=None, maxsplit=None):通過指定分隔字元對字串進行切片。string = "haha lala gege"print(string.split(' '))#輸出:['haha', 'lala', 'gege']print(string.split(' ', 1 ))#輸出: ['haha', 'lala gege']  # rsplit(self, sep=None, maxsplit=None):通過指定分隔字元對字串從右進行切片。string = "haha lala gege"print(string.rsplit(' '))#輸出:['haha', 'lala', 'gege']print(string.rsplit(' ', 1 ))#輸出: ['haha lala', 'gege']  # rstrip(self, chars=None):刪除 string 字串末尾的指定字元(預設為空白格).string = "     My name is yue.      "print(string.rstrip())#輸出:     My name is yue.  # strip(self, chars=None):移除字串頭尾指定的字元(預設為空白格)。string = "        My name is yue.      "print(string.strip())#輸出:My name is yue.  # splitlines(self, keepends=False):按照行('\r', '\r\n', \n')分隔,返回一個包含各行作為元素的列表,如果參數 keepends 為 False,不包含分行符號,如果為 True,則保留分行符號。str1 = 'ab c\n\nde fg\rkl\r\n'print(str1.splitlines())# 輸出:['ab c', '', 'de fg', 'kl']str2 = 'ab c\n\nde fg\rkl\r\n'print(str2.splitlines(True))# 輸出:['ab c\n', '\n', 'de fg\r', 'kl\r\n']  # startswith(self, prefix, start=None, end=None):檢查字串是否是以指定子字串開頭,如果是則返回 True,否則返回 False。如果參數 beg 和 end 指定值,則在指定範圍內檢查。string = "My name is yue.      "print(string.startswith('My'))#輸出:Truestring = "My name is yue."print(string.startswith('yue'))#輸出:False  # swapcase(self):對字串的大小寫字母進行轉換。string = "My Name Is Yue."print(string.swapcase())#輸出:mY nAME iS yUE.  # title(self):返回"標題化"的字串,就是說所有單詞都是以大寫開始,其餘字母均為小寫(見 istitle())。string = "my name is yue,my age is 18."print(string.title())#輸出:My Name Is Yue,My Age Is 18.  # translate(self, table, deletechars=None):根據參數table給出的表(包含 256 個字元)轉換字串的字元, 要過濾掉的字元放到 del 參數中。from string import maketransstr = "aoeiu"num = "12345"trantab = maketrans(str, num)string = "my name is yue"print(string.translate(trantab))# 輸出:my n1m3 4s y53  # upper(self):將字串中的小寫字母轉為大寫字母。string = "my name is yue,my age is 18."print(string.upper())#輸出:MY NAME IS YUE,MY AGE IS 18.  # zfill(self, width):返回指定長度的字串,原字串靠右對齊,前面填充0。string = "my name is yue."print(string.zfill(18))#輸出:000my name is yue.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.