Python處理字符串的常用函數實例總結
前言
今天我們說瞭字符串的基礎,格式化,這次我們講解字符串的常用函數,不要錯過!
前兩篇都在本文同個專欄,歡迎關註。下面開始講解。
字符串都有哪些操作?
實際開發都有這些需求:
第一大類:判斷識別字符串
- 判斷字符串屬於那種字面類型(數字,全字母,其他)
- 判斷字符串包含某些結構(數字大寫,局部子串,子串出現頻次等)
第二類:字符串編輯的操作(生成新字符串)
- 字符串的替換/合並/填充等
- 字典替換,填充0值,清空操作等
第三類:字符串跟字節串的互轉。
這類操作通常發生在數據跨程序/跨服務器傳輸,我們傳輸bytes,然後獲取轉string類型。
第一類 判斷識別字符串
學委準備瞭下面的代碼:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/10/30 10:13 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @XueWeiTag: CodingDemo # @File : string_funs_cat1.py # @Project : hello import sys slogan = "keep studying, keep coding, I am Levin" # 判斷結構 print("算某個子串出現數次: slogan.count('keep') = ", slogan.count('keep')) print("找某個子串首次出現的下標: slogan.find('keep') = ", slogan.find('keep')) print("找某個子串最後出現的下標: slogan.rfind('keep') = ", slogan.rfind('keep')) print("找某個子串下標: slogan.index('keep') = ", slogan.index('keep')) print("找某個子串下標: slogan.rindex('keep') = ", slogan.rindex('keep')) print("是否'keep'開頭的字符串: slogan.startswith('keep') = ", slogan.startswith('keep')) print("是否'keep'結束的字符串: slogan.endswith('keep') = ", slogan.endswith('keep')) # 字符串屬性相關 print("字符串長度: len(slogan) = ", len(slogan)) print("字符串是否都是空格: slogan.isspace() = ", slogan.isspace()) print("字符串是否大寫: slogan.isupper() = ", slogan.isupper()) print("字符串是否小寫: slogan.islower() = ", slogan.islower()) print("字符串是否為每個詞首字母都大寫: slogan.istitle() = ", slogan.istitle()) # 判斷字符串數據類型 print("字符串是否全為字母: slogan.isalpha() = ", slogan.isalpha()) print("字符串是否全為數字: slogan.isalnum() = ", slogan.isalnum()) print("字符串是否數字: slogan.isnumeric() = ", slogan.isnumeric()) print("字符串是否浮點數: slogan.isdecimal() = ", slogan.isdecimal()) print("字符串是否為空格串: slogan.isspace() = ", slogan.isspace())
讀者可以直接復制運行代碼,學委補充瞭運行效果圖:
第二類 字符串編輯的操作
下面學委準備瞭一些代碼展示:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/10/30 10:13 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @XueWeiTag: CodingDemo # @File : string_funs_cat2.py # @Project : hello import sys slogan = "keep studying, keep coding, I am Levin" print("首字母大寫: slogan.capitalize() = ", slogan.capitalize()) print("全部字母大寫: slogan.upper() = ", slogan.upper()) print("全部字母小寫: slogan.lower() = ", slogan.lower()) print("轉為首字母都大寫(標題風格): slogan.title() = ", slogan.title()) print("大小寫逆轉: slogan.swapcase() = ", slogan.swapcase()) table = slogan.maketrans({"e": "5"}) print("字符串替換表: slogan.translate(table) = ", slogan.translate(table)) # 字符串替換,合並,填充等 print("替換tabs為n個空格: 'hello\t學委'.expandtabs(4) = '", "hello\t學委".expandtabs(4)) print("左子串來串聯傳入的列表: ' '.join(slogan) = '", " ".join(slogan)) print("替換子串: ' '.replace(first, second) = '", slogan.replace("e", "11")) print("填充0值: slogan.rzfill(2)= '", slogan.zfill(50)) print("填充#值: slogan.rjust(50,"#")= '", slogan.rjust(50,"#")) print("填充#值: slogan.ljust(50,"#")= '", slogan.ljust(50,"#")) print("移除首尾空格: slogan.strip()= '", slogan.strip()) data = slogan.split("e") print("split slogan into data= ", data)
運行效果如下:
第三類:字符串跟字節串的互轉
下面學委準備瞭一些代碼展示:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/10/30 10:13 上午 # @Author : LeiXueWei # @CSDN/Juejin/Wechat: 雷學委 # @XueWeiTag: CodingDemo # @File : string_funs_cat3.py # @Project : hello import sys slogan = "keep studying, keep coding, I am Levin" bytes = slogan.encode("utf-8") print("type of encoded string = ", type(bytes)) # 註意python的string類型沒有decode函數,該函數屬於bytes類型對象特有!!! print("type of decoded byte = ", type(bytes.decode("utf-8"))) print("type of decoded byte = ", bytes.decode("utf-8"))
運行效果如下:
總結
其實學委還漏瞭幾個函數,但是不想介紹它們瞭。
學習編程不是去記憶,但是也並非啥都不看都靠感覺。
我喜歡下面這句話:
讀書破萬卷,下筆如有神!
精心準備的代碼,讀者運行一下,自己感悟!(註釋寫的很清楚瞭)
到此這篇關於Python處理字符串的常用函數的文章就介紹到這瞭,更多相關Python處理字符串函數內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!