python字符串切片及常用方法示例詳解
一、切片
切片:指對操作的對象截取其中一部分的操作,字符串、列表、元組都支持切片操作
語法:序列[開始位置下標(biāo):結(jié)束位置下標(biāo):步長] ,不包含結(jié)束位置下標(biāo)數(shù)據(jù),步長為選取間隔,正負(fù)均可,默認(rèn)為1
舉例如下:
str = 'abcdefg_a' print(str[1:6:2], str[2:6], str[:3], str[3:], str[:]) print(str[::2], str[:-2], str[-6:-2], str[::-2], str[::-1]) print(str[-2:], str[2:-2], str[-2::-2], str[:-2:2], str[2:-2:2]) 輸出: bdf cdef abc defg_a abcdefg_a acega abcdefg defg ageca a_gfedcba _a cdefg _fdb aceg ceg
二、常用方法
2.1 查找
查找字符串:即查找子串在字符串中的位置或出現(xiàn)的次數(shù)
- find():檢測某個字串是否包含在某個字符串中,若存在則返回該子串開始位置下標(biāo),否則返回-1
- 語法:字符串序列.find(子串,開始位置下標(biāo),結(jié)束位置下標(biāo))
- index():檢測某個子串是否包含在某個字符串中,若存在則返回該子串開始位置下標(biāo),否則報異常
- 語法:字符串序列.index(子串,開始位置下標(biāo),結(jié)束位置下標(biāo))
- rfind():和find()功能相同,但查找方向為右側(cè)開始,即返回子串最后出現(xiàn)位置
- rindex():和index()功能相同,但查找方向為右側(cè)開始,即返回子串最后出現(xiàn)位置
- count():返回某個子串在字符串中出現(xiàn)的次數(shù)
舉例如下:
str = 'abcdefg_a' print('-------------------查找-------------------') print(str.find('c'), str.find('fg', 2, ), str.find('a', 2), str.find('h')) print(str.index('c'), str.index('fg', 2, ), str.index('a', 2)) print(str.find('a'), str.rfind('a'), str.index('a'), str.rindex('a'), str.count('a')) print(str.index('h')) 輸出: -------------------查找------------------- 2 5 8 -1 2 5 8 0 8 0 8 2 ValueError: substring not found
2.2 修改
修改字符串:通過函數(shù)形式修改字符串中的數(shù)據(jù)
- replace():替換
- 語法:字符串序列.replace(舊子串,新子串,最大替換次數(shù))
- split():按指定字符分割字符串
- 語法:字符串序列.split(分割字符,分割次數(shù)) # 返回數(shù)據(jù)個數(shù)為分割次數(shù)+1
- join():用一個字符或子串合并字符串,即將多個字符串合并為一個新的字符串
- 語法:字符或子串.join(多字符串組成的序列)
- capitalize():將字符串第一個字符轉(zhuǎn)為大寫,轉(zhuǎn)換后僅首字符大寫,其余均小寫
- 語法:字符串序列.capitalize()
- title():將字符串每個單詞首字母轉(zhuǎn)為大寫
- lower():將字符串中大寫轉(zhuǎn)小寫
- upper():將字符串中小寫轉(zhuǎn)大寫
- swapcase():翻轉(zhuǎn)字符串中大小寫
- partition('分隔符'):根據(jù)指定分隔符將字符串分割,返回三元元組,組成為左子串、分隔符、右子串
- min(str):返回字符串str中最小字母
- max(str):返回字符串str中最大字母
- zfill(width):輸出指定長度為width的字符串,右對齊,不足前面補(bǔ)0,超出指定長度則原樣輸出
- lstrip():刪除字符串左側(cè)空格字符
- rstrip():刪除字符串右側(cè)空格字符
- strip():刪除字符串兩側(cè)空格字符
- ljust():字符串左對齊,并用指定字符(默認(rèn)空格)填充至對應(yīng)長度
- 語法:字符串序列.ljust(長度,填充字符)
- rjust():字符串右對齊,并用指定字符(默認(rèn)空格)填充至對應(yīng)長度
- 語法:字符串序列.rjust(長度,填充字符)
- center():居中對齊,并用指定字符(默認(rèn)空格)填充至對應(yīng)長度
- 語法:字符串序列.center(長度,填充字符)
舉例如下:
print('--------------修改--------------') str1 = 'hello python and hello IT and hello world and hello YX !' print(str1.replace('and','&&')) print(str1.split('and'), str1.split('and', 2)) l = ['Hello', 'world', '!'] t = ('Hello', 'python', '!') print('_'.join(l), ' '.join(t)) # 用下劃線_和空格連接 print(str1.capitalize()) # 首字符轉(zhuǎn)為大寫,其余均小寫 print(str1.title()) # 每個單詞首字母轉(zhuǎn)為大寫 str2 = ' Hello World ! ' print(str2.lower(), str2.upper(), str2.swapcase()) # 大寫轉(zhuǎn)小寫,小寫轉(zhuǎn)大寫,翻轉(zhuǎn)大小寫 print(str2.partition('rl'), str2.partition('o')) # 根據(jù)指定分隔符將字符串分割,返回三元元組 print(min(str2), max(str2), ord(min(str2)), ord(max(str2))) # str2中最小為空格對應(yīng)十進(jìn)制32,最大為r對應(yīng)114 print(str2.zfill(21)) # 輸出指定長度為21的字符串,右對齊,不足前面補(bǔ)0,超出指定長度則原樣輸出 print(str2.lstrip(), str2.rstrip(), str2.strip()) # 清除字符串左、右、兩邊空格字符 str3 = 'hello!' print(str3.ljust(13, '*'), str3.rjust(13, '*'), str3.center(14, '*')) 輸出: --------------修改-------------- hello python && hello IT && hello world && hello YX ! ['hello python ', ' hello IT ', ' hello world ', ' hello YX !'] ['hello python ', ' hello IT ', ' hello world and hello YX !'] Hello_world_! Hello python ! Hello python and hello it and hello world and hello yx ! Hello Python And Hello It And Hello World And Hello Yx ! hello world ! HELLO WORLD ! hELLO wORLD ! (' Hello Wo', 'rl', 'd ! ') (' Hell', 'o', ' World ! ') r 32 114 00 Hello World ! Hello World ! Hello World ! Hello World ! hello!******* *******hello! ****hello!****
2.3 判斷
- startswith():檢查字符串是否以指定子串開頭,若是返回True,否則返回False,設(shè)置開始和就結(jié)束位置下標(biāo),則在指定范圍內(nèi)檢查
- 語法:字符串序列.startswith(子串,開始位置下標(biāo),結(jié)束位置下標(biāo))
- endswith():檢查字符串是否以指定子串結(jié)尾,是返回True,否則返回False,設(shè)置開始和就結(jié)束位置下標(biāo),則在指定范圍內(nèi)檢查
- 語法:字符串序列.endswith(子串,開始位置下標(biāo),結(jié)束位置下標(biāo))
- isalpha():若字符串至少有一個字符并所有字符都是字母則返回True,否則返回False
- isdigit():若字符串只包含數(shù)字則返回True否則返回False
- isalnum():若字符串至少有一個字符且所有字符都是字母或數(shù)字則返回True,否則返回False
- isspace():若字符串只包含空格,則返回True,否則返回False
舉例如下:
print('---------------判斷----------------') str3 = 'hello!' print(str3.startswith('he'), str3.startswith('she'), str3.startswith('he',2,)) print(str3.endswith('!'), str3.endswith('。'), str3.endswith('!', 2, 5)) print(str3.isalpha(),str3.isalnum(), str3.isdigit(), str3.isspace()) 輸出: ---------------判斷---------------- True False False True False False False False False False
導(dǎo)航:http://xqnav.top/
到此這篇關(guān)于python字符串切片及常用方法的文章就介紹到這了,更多相關(guān)python字符串切片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python調(diào)用機(jī)器喇叭發(fā)出蜂鳴聲(Beep)的方法
這篇文章主要介紹了python調(diào)用機(jī)器喇叭發(fā)出蜂鳴聲(Beep)的方法,實例分析了Python調(diào)用winsound模塊的使用技巧,需要的朋友可以參考下2015-03-03win與linux系統(tǒng)中python requests 安裝
requests是Python的一個HTTP客戶端庫,跟urllib,urllib2類似,今天我們主要來談?wù)剋in與linux系統(tǒng)中python requests的安裝方法以及使用指南2016-12-12Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法
這篇文章主要介紹了Python使用Flask框架獲取當(dāng)前查詢參數(shù)的方法,實例分析了query_string獲取查詢參數(shù)的技巧,需要的朋友可以參考下2015-03-03