python字符串不可變數(shù)據(jù)類型
一、截取子串-切片
方法:字符串名[初始位置:結(jié)束位置:步長]
str1 = 'abcdefg' print(str1[:]) # abcdefg print(str1[0:2]) # ab 左閉右開 print(str1[0:2:1]) # ab 默認步長為1 print(str1[:3]) # abc print(str1[0:]) # abcdefg print(str1[::-1]) # gfedcba print(str1[-4:-1]) #def -1代表最右端 # print(str1[-4:-1:-1]) # 盡量避免這樣寫
二、查找子串及數(shù)量
方法:
- 1.
字符串名.index(子串,起始位置,末位置)
---對應(yīng) rindex 從右往左數(shù) - 2.
字符串名.find(子串,起始位置,末位置)
--對應(yīng)rfind從右往左數(shù) - 3.
字符串名.count(子串,其實位置,末位置)
str1 = 'my name is fl and my pet is a parrot' print(str1.find('is')) # 8 print(str1.find('is', 9, 30)) # 25 print(str1.find('dog')) # -1 --find如果沒有查找到對應(yīng)的子串則返回-1 ? print(str1.index('is')) # 8 print(str1.index('is', 9, 30)) # 25 # print(str1.index('dog')) # -1 --index如果沒有查找到對應(yīng)的子串則報錯 ? print(str1.rindex('is')) # 25 print(str1.rfind('is')) # 25 ? print(str1.count('is')) # 2 print(str1.count('dog')) # 0 print(str1.count('is', 0, 15)) # 1
三、字符串的替換、分割以及合并
1. 字符串的替換:字符串名.replace(原串,替換成的串, 替換次數(shù))
str1 = 'Food and Fruit and Animal' new_str = str1.replace('and', 'he') # Food he Fruit he Animal new_str = str1.replace('and', 'he', 1) # Food he Fruit and Animal
2. 字符串的分割:字符串名.split(分割的串, 分割次數(shù))
str1 = 'Food and Fruit and Animal' new_str = str1.split('and') # ['Food ', ' Fruit ', ' Animal'] new_str = str1.split('and', 1) # ['Food ', ' Fruit and Animal']
3. 字符串的合并:合并符號.join(列表名)
mylist = ['I', 'Love', 'You'] new_str = ' '.join(mylist) # I Love You new_str = '..'.join(mylist) # I..Love..You
四、字母的大小寫
- 1. 僅首字母大寫:字符串名.capitalize()
- 2. 每個單詞的首字母都變成大寫:字符串名.title()
- 3. 將每個小寫字母都變成大寫字母:字符串名.upper()
- 4. 將每個大寫字母都變成小寫字母:字符串名.lower()
str1 = 'oh , my dear , I love you' new_str = str1.capitalize() # Oh , my dear , i love you new_str = str1.title() # Oh , My Dear , I Love You new_str = str1.upper() # OH , MY DEAR , I LOVE YOU new_str = str1.lower() # oh , my dear , i love you
五、刪除側(cè)邊的空白
- 1. 刪除左側(cè)空白:字符串名.lstrip()
- 2. 刪除右側(cè)空白:字符串名.rstrip()
- 3. 刪除兩側(cè)空白:字符串名.strip()
str1 = " ? come on baby , it's true ?" new_str = str1.lstrip() # come on baby , it's true new_str = str1.rstrip() # ? ?come on baby , it's true new_str = str1.strip() # come on baby , it's true
六、對齊方式
方法:字符串名.ljust
(整體字符長度, 用于填補的符號)
str1 = 'abcd' new_str = str1.ljust(10, '.') # abcd...... new_str = str1.ljust(10) # abcd(后面有六個空格) new_str = str1.rjust(10, '/') # //abcd new_str = str1.center(10, '.') # ...abcd...
七、判斷首位字符是否正確(返回布爾值)
- 1. 字符串名.
startswith
(子串, 起始位置, 末位置) - 2. 字符串名.
endswith
(子串, 起始位置, 末位置)
str1 = 'hello world' print(str1.startswith('he')) # True print(str1.startswith('ho')) # False print(str1.endswith('ho')) # False print(str1.endswith('world')) # True
八、判斷字母、數(shù)以及空格
- 1. 判斷是否所有都為字母:字符串名.isalpha()
- 2. 判斷是否所有都為數(shù)字:字符串名.isdigit()
- 3. 判斷是否所有都為字母或數(shù)字或其組合:字符串名.isalnum()
- 4. 判斷是否所有都為空格:字符串名.isspace()
str1 = 'hello world' print(str1.isalpha()) # False str1 = 'abc' print(str1.isalpha()) # True ? str2 = '123 45' print(str2.isdigit()) # False str2 = '123' print(str2.isdigit()) # True ? str3 = 'abc123' print(str3.isalnum()) # True str3 = 'abc 123' print(str3.isalnum()) # False ? str4 = '123 ' print(str4.isspace()) # False str4 = ' ? ' print(str4.isspace()) # True
到此這篇關(guān)于python
字符串不可變數(shù)據(jù)類型的文章就介紹到這了,更多相關(guān)python字符串內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python?tkinter中四個常用按鈕的用法總結(jié)
tkinter中有四個控件被冠以Button之名,分別是:Button,?Checkbutton,?Radiobutton,?Menubutton,下面小編就來和大家聊聊它們的具體用法,感興趣的可以學(xué)習(xí)一下2023-09-09Python列表list操作相關(guān)知識小結(jié)
今天,本喵帶大家仔細溫習(xí)一下Python的列表,溫故而知新,不亦說乎,需要的朋友可以參考下2020-01-01python2.7實現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù)
這篇文章主要為大家詳細介紹了python2.7實現(xiàn)爬蟲網(wǎng)頁數(shù)據(jù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Python構(gòu)造自定義方法來美化字典結(jié)構(gòu)輸出的示例
這篇文章主要介紹了用Python構(gòu)造自定義方法來美化字典結(jié)構(gòu)輸出的示例,原理就是利用遞歸法來拼接字符串,需要的朋友可以參考下2016-06-06python中用Scrapy實現(xiàn)定時爬蟲的實例講解
在本篇文章里小編給大家整理的是一篇關(guān)于python中用Scrapy實現(xiàn)定時爬蟲的實例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01