字符串類型內建方法歸納總結

版本:python3.6

在查看bulitins的內建函數時,對字符串的內建方法進行了分類總結。

類別 方法描述示例
查找string.find(sub, start=None, end=None)

在指定的範圍內查找子串,

找到則返回最小的index;

未找到則返回-1

mystr = 'hello world '
mystr.find('w') #返回6
mystr.find('x') #返回-1
string.rfind(sub, start=None, end=None)

在指定的範圍內查找子串,

找到則返回最大的index;

未找到則返回-1

mystr = 'hello world '
mystr.find('l') #返回2
mystr.rfind('l') #返回9

string.count(sub, start=None, end=None
)

在指定的範圍內查找子串,

找到則返回找的子串個數

未找到則返回0

mystr = 'hello world '
mystr.count('w') #返回3
mystr.count('x') #返回0
string.index(sub, start=None, end=None)

在指定的範圍內查找子串,

找到則返回最小的index;

未找到則返回ValueError的錯誤

mystr = 'hello world '
mystr.index('w') #返回6
mystr.index('x') 
#返回ValueError:substring not found
string.rindex(sub, start=None, end=None)

在指定的範圍內查找子串,

找到則返回最大的index;

未找到則返回ValueError的錯誤

mystr = 'hello world '
mystr.index('l')  #返回2
mystr.rindex('l')  #返回9
填充、拼接字符string.center(width,fillchar)返回長度爲width的字符串,原字符串居中,可指定填充的字符,填充的字符串均分在左右兩側
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的字符串,原字符串右對齊,左側填充0
mystr = 'hello world '
mystr.zfill(17)  
#返回00000hello world

string.ljust(width,fillchar)

返回長度爲width的字符串,原字符串左對齊,右側填充指定的字符
mystr = 'hello world '
mystr.ljust(18,'\'') 
#返回hello world ''''''
string.rjust(width,fillchar)返回長度爲width的字符串,原字符串右對齊,左側填充指定的字符
mystr = 'hello world '
mystr.rjust(18,'\'') 
#返回''''''hello world
轉換字母大小寫string.capitalize()將首字母轉換爲大寫
mystr = 'hello world '
mystr.capitalize() 
#返回Hello world
string.upper()將字符串中的所有字母轉換爲大寫
mystr = 'hello world '
mystr.upper() 
#返回HELLO WORLD
string.lower()將字符串中的所有字母轉換爲小寫
mystr = 'Hello World '
mystr.lower() 
#返回hello world
string.swapcase()翻轉字符串中的大小寫
mystr = 'Hello World '
mystr.swapcase() 
#返回hELLO wORLD
string.title()將所有單詞的首字母轉換爲大寫
mystr = 'hello world, it\'s fantastic'
mystr.title()
#返回 Hello World, It'S Fantastic
刪除字符串的空格string.strip()刪除字符串左右兩側的空格
mystr = '  hello world  '
mystr.strip()
#返回hello world
string.lstrip()刪除字符串左側的空格
mystr = '  hello world  '
mystr.lstrip()
#返回hello world
string.rstrip()刪除字符串右側的空格
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')分隔,返回一個包含各行作爲元素的列表,如果參數 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中至少有一個字符串且所有字符都是字母或數字,則返回True,否則返回Flase
mystr1 = ' hello, world ! '
mystr2 = 'hello123'
mystr1.isalnum()  #返回False
mystr2.isalnum()  #返回True
string.isdemical()str中只包含十進制數字則返回True,否則返回False
mystr1 = '1091A '
mystr2 = '1091'
mystr1.isdecimal()  #返回False
mystr2.isdecimal()  #返回True
string.isdigit()str中只包含數字則返回True,否則返回False
mystr1 = '1091A '
mystr2 = '1091'
mystr1.isdigit()  #返回False
mystr2.isdigit()  #返回True
string.isnumeric()str中只包含數字字符則返回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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章