版本:python3.6

在查看bulitins的內(nèi)建函數(shù)時,對字符串的內(nèi)建方法進行了分類總結(jié)。
| 類別 | 方法 | 描述 | 示例 |
| 查找 | string.find(sub, start=None, end=None) | 在指定的范圍內(nèi)查找子串, 找到則返回最小的index; 未找到則返回-1 | mystr = 'hello world '
mystr.find('w') #返回6
mystr.find('x') #返回-1 |
| string.rfind(sub, start=None, end=None) | 在指定的范圍內(nèi)查找子串, 找到則返回大的index; 未找到則返回-1 | mystr = 'hello world '
mystr.find('l') #返回2
mystr.rfind('l') #返回9 | |
string.count(sub, start=None, end=None | 在指定的范圍內(nèi)查找子串, 找到則返回找的子串個數(shù); 未找到則返回0 | mystr = 'hello world '
mystr.count('w') #返回3
mystr.count('x') #返回0 | |
| string.index(sub, start=None, end=None) | 在指定的范圍內(nèi)查找子串, 找到則返回最小的index; 未找到則返回ValueError的錯誤 | mystr = 'hello world '
mystr.index('w') #返回6
mystr.index('x')
#返回ValueError:substring not found | |
| string.rindex(sub, start=None, end=None) | 在指定的范圍內(nèi)查找子串, 找到則返回大的index; 未找到則返回ValueError的錯誤 | mystr = 'hello world '
mystr.index('l') #返回2
mystr.rindex('l') #返回9 | |
| 填充、拼接字符 | string.center(width,fillchar) | 返回長度為width的字符串,原字符串居中,可指定填充的字符,填充的字符串均分在左右兩側(cè) | mystr = 'hello world ' mystr.center(18,'\'') #返回'''hello world ''' |
| string.join(sep) | 以字符串為分隔符,將sep中所有的元素合并為一個新的字符串 | mystr = ' hello world '
mystr.join('and')
#返回a hello world n hello world d | |
| string.zfill(width) | 返回長度為width的字符串,原字符串右對齊,左側(cè)填充0 | mystr = 'hello world ' mystr.zfill(17) #返回00000hello world | |
string.ljust(width,fillchar) | 返回長度為width的字符串,原字符串左對齊,右側(cè)填充指定的字符 | mystr = 'hello world ' mystr.ljust(18,'\'') #返回hello world '''''' | |
| string.rjust(width,fillchar) | 返回長度為width的字符串,原字符串右對齊,左側(cè)填充指定的字符 | mystr = 'hello world ' mystr.rjust(18,'\'') #返回''''''hello world | |
| 轉(zhuǎn)換字母大小寫 | string.capitalize() | 將首字母轉(zhuǎn)換為大寫 | mystr = 'hello world ' mystr.capitalize() #返回Hello world |
| string.upper() | 將字符串中的所有字母轉(zhuǎn)換為大寫 | mystr = 'hello world ' mystr.upper() #返回HELLO WORLD | |
| string.lower() | 將字符串中的所有字母轉(zhuǎn)換為小寫 | mystr = 'Hello World ' mystr.lower() #返回hello world | |
| string.swapcase() | 翻轉(zhuǎn)字符串中的大小寫 | mystr = 'Hello World ' mystr.swapcase() #返回hELLO wORLD | |
| string.title() | 將所有單詞的首字母轉(zhuǎn)換為大寫 | mystr = 'hello world, it\'s fantastic' mystr.title() #返回 Hello World, It'S Fantastic | |
| 刪除字符串的空格 | string.strip() | 刪除字符串左右兩側(cè)的空格 | mystr = ' hello world ' mystr.strip() #返回hello world |
| string.lstrip() | 刪除字符串左側(cè)的空格 | mystr = ' hello world ' mystr.lstrip() #返回hello world | |
| string.rstrip() | 刪除字符串右側(cè)的空格 | mystr = ' hello world ' mystr.rstrip() #返回 hello world | |
| 拆分字符串 | string.partition(sep) | 以指定的字符,將字符串分為為head,sep,tail 3部分;如果沒有匹配的字符,返回字符串和2個空串 | mystr = ' hello world '
mystr.partition('w')
#返回(' hello ', 'w', 'orld ')
mystr.partition(';')
#返回(' hello world ', '', '') |
| string.rpartition(sep) | 以指定的字符,將字符串分為為head,sep,tail 3部分;如果沒有匹配的字符,返回2個空串和字符串 | mystr = ' hello world '
mystr.rpartition('w')
#返回(' hello ', 'w', 'orld ')
mystr.rpartition(';')
#返回('', '',' hello world ') | |
| string.split(sep=None, maxsplit=-1) | 以sep為分隔符,切片字符串 | mystr = ' hello world ' mystr.split(sep='l',maxsplit=2) #返回[' he', '', 'o world '] | |
| string.rsplit(sep=None, maxsplit=-1 ) | 以sep為分隔符,切片字符串 | mystr = ' hello world ' mystr.rsplit(sep='l',maxsplit=2) #返回[' hel', 'o wor', 'd '] | |
| string.splitlines(keepends=None ) | 按照行('\r', '\r\n', \n')分隔,返回一個包含各行作為元素的列表,如果參數(shù) keepends 為 False,不包含換行符,如果為 True,則保留換行符 | mystr = ' hello world ,\n it \'s fantastic' mystr.splitlines() #返回[' hello world ,', " it 's fantastic"] | |
| 替換 | string.replace(old, new, count=None ) | 字符串替換 | mystr = ' hello world '
mystr.replace('world','Beijing')
#返回 hello Beijing |
| is系列 | string.isalpha() | str中至少有一個字符串且所有字符都是字母,則返回True,否則返回Flase | mystr1 = ' hello, world ' mystr2 = 'hello' mystr1.isalpha() #返回False mystr2.isalpha() #返回True |
| string.isalnum() | str中至少有一個字符串且所有字符都是字母或數(shù)字,則返回True,否則返回Flase | mystr1 = ' hello, world ! ' mystr2 = 'hello123' mystr1.isalnum() #返回False mystr2.isalnum() #返回True | |
| string.isdemical() | str中只包含十進制數(shù)字則返回True,否則返回False | mystr1 = '1091A ' mystr2 = '1091' mystr1.isdecimal() #返回False mystr2.isdecimal() #返回True | |
| string.isdigit() | str中只包含數(shù)字則返回True,否則返回False | mystr1 = '1091A ' mystr2 = '1091' mystr1.isdigit() #返回False mystr2.isdigit() #返回True | |
| string.isnumeric() | str中只包含數(shù)字字符則返回True,否則返回False | mystr1 = '1091A ' mystr2 = '1091' mystr1.isnumeric() #返回False mystr2.isnumeric() #返回True | |
| string.istitle() | str是標題化的則返回True,否則返回False | mystr1 = 'hello world ' mystr2 = 'Hello World' mystr1.istitle() #返回False mystr2.istitle() #返回True | |
| string.islower () | 所有字符都是小寫則返回True,否則返回False | mystr1 = 'Hello world ' mystr2 = 'hello world' mystr1.islower() #返回False mystr2.islower() #返回True | |
| string.isupper() | 所有字符都是大寫則返回True,否則返回False | mystr1 = 'Hello world ' mystr2 = 'hello world' mystr1.isupper() #返回False (mystr2.swapcase()).isupper() #返回True |
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
當前標題:字符串類型內(nèi)建方法歸納總結(jié)-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article18/digsdp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、關鍵詞優(yōu)化、用戶體驗、手機網(wǎng)站建設、網(wǎng)站營銷、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容