一、字元串基礎操作
Python中字元串是不可變對象,但是可以通過一些基本操作對字元串進行修改、拼接、截取等操作。具體包括:
1、字元串索引:通過索引獲取字元串中某個位置的字元,如s=’Hello’,s[0]就是’H’
s = 'Hello' print(s[0])
2、字元串切片:通過切片獲取部分位置的字元,如s=’Hello’,s[1:3]就是’el’
s = 'Hello' print(s[1:3])
3、字元串拼接:使用”+”操作符將兩個或多個字元串拼接在一起,如s1=’Hello’,s2=’World’,s1+s2就是’HelloWorld’
s1 = 'Hello' s2 = 'World' print(s1 + s2)
4、字元串重複:使用”*”操作符將一個字元串重複多次,如s=’Hello’,3*s就是’HelloHelloHello’
s = 'Hello' print(3 * s)
二、字元串格式化
Python中提供了多種字元串格式化的方式,具體包括:
1、字元串格式化表達式:使用”%”操作符對字元串進行格式化,如s = ‘I am %s, and %d years old.’ % (‘Tom’, 18),結果為’I am Tom, and 18 years old.’
s = 'I am %s, and %d years old.' % ('Tom', 18) print(s)
2、str.format方法:使用{}佔位符進行字元串格式化,如s = ‘I am {}, and {} years old.’.format(‘Tom’, 18),結果為’I am Tom, and 18 years old.’
s = 'I am {}, and {} years old.'.format('Tom', 18) print(s)
3、f-string方法:使用f前綴對字元串進行格式化,如name = ‘Tom’, age = 18,s = f’I am {name}, and {age} years old.’,結果為’I am Tom, and 18 years old.’
name = 'Tom' age = 18 s = f'I am {name}, and {age} years old.' print(s)
三、字元串常用方法
Python中提供了多個字元串處理的方法,常用方法包括:
1、字元串長度len():獲取字元串的長度
s = 'Hello' print(len(s))
2、字元串查找find()方法:查找指定字元串出現的位置,如果找到則返回索引值,否則返回-1
s = 'Hello' print(s.find('llo')) print(s.find('xxx'))
3、字元串替換replace()方法:將指定字元串替換為另一個字元串
s = 'Hello' print(s.replace('llo', 'rld'))
4、字元串分割split()方法:將字元串按照指定分隔符進行切分,返回一個列表
s = 'Hello,World' print(s.split(','))
5、字元串大小寫轉換lower()/upper()方法:將字元串全部轉為小寫/大寫
s = 'HeLLo' print(s.lower()) print(s.upper())
四、字元串正則表達式
正則表達式是一種用於匹配文本的模式,Python中提供了re模塊用於支持正則表達式的處理。常用正則表達式符號包括:
1、字元匹配:如s = ‘apple’,re.findall(‘a.’, s)返回[‘ap’],’.’表示匹配任意一個字元
import re s = 'apple' print(re.findall('a.', s))
2、字符集合匹配:如s = ‘apple’,re.findall(‘[aeiou]’, s)返回[‘a’, ‘e’],'[aeiou]’表示匹配集合中任意一個字元
import re s = 'apple' print(re.findall('[aeiou]', s))
3、重複匹配:如s = ‘aabbcc’,re.findall(‘a+’, s)返回[‘aa’],’+’表示匹配前面的字元(或字符集合)重複一次或多次
import re s = 'aabbcc' print(re.findall('a+', s))
4、分組與引用:如s = ‘apple:banana’,re.findall(‘(\w+):(\w+)’, s)返回[(‘apple’, ‘banana’)],'(\w+)’表示匹配一個單詞字元(字母、數字、下劃線)一次或多次,並將匹配結果以元組的形式返回
import re s = 'apple:banana' print(re.findall('(\w+):(\w+)', s))
五、字元串加解密
加解密是指將原始數據進行加密或解密處理,保障數據傳輸和存儲的安全性。Python中提供了多個常用的加解密模塊,包括:
1、base64模塊:將二進位數據進行base64編碼或解碼
import base64 data = b'Hello' # base64編碼 b64_data = base64.b64encode(data) print(b64_data) # base64解碼 origin_data = base64.b64decode(b64_data) print(origin_data)
2、hashlib模塊:常用哈希演算法包括MD5、SHA1等,將原始數據進行哈希處理,並生成摘要信息
import hashlib data = b'Hello' # MD5哈希 md5 = hashlib.md5() md5.update(data) print(md5.hexdigest()) # SHA1哈希 sha1 = hashlib.sha1() sha1.update(data) print(sha1.hexdigest())
3、pycryptodome模塊:常用對稱加密演算法包括AES、DES等,使用密碼對原始數據進行加密、解密處理
from Crypto.Cipher import AES from Crypto.Random import get_random_bytes data = b'HelloWorld' # 隨機生成16位元組的密碼 key = get_random_bytes(16) # 使用AES CBC模式進行加密 cipher = AES.new(key, AES.MODE_CBC) ct_data = cipher.encrypt(data) print(ct_data) # 使用AES CBC模式進行解密 cipher = AES.new(key, AES.MODE_CBC, iv=cipher.iv) pt_data = cipher.decrypt(ct_data) print(pt_data)
六、字元串處理實例
實例:給定一個字元串,統計其中英文單詞出現的頻率,並按照頻率從高到低進行排序輸出
import re s = 'The quick brown fox jumps over the lazy dog' # 先將字母數字字元外的其他字元替換為空格 s = re.sub('[^a-zA-Z0-9]', ' ', s) # 將字元串按照空格進行分割,並統計單詞出現的頻率 freq = {} for word in s.split(): freq[word] = freq.get(word, 0) + 1 # 按照頻率從高到低進行排序輸出 sorted_freq = sorted(freq.items(), key=lambda x: x[1], reverse=True) for word, count in sorted_freq: print(f'{word}: {count}')
七、結語
以上僅是Python中字元串處理的一些常用操作和使用示例,Python中還有很多強大的字元串處理模塊和方法,能夠滿足各種複雜的業務需求。
原創文章,作者:QWEW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/133304.html