# 敏感詞過濾
text ="這個產品太垃圾了!"
bad_words =["垃圾","廢物","差勁"]for word in bad_words:text = text.replace(word,"**")print(text)# 輸出: "這個產品太**了!"
三、字符串判斷方法
1. 內容判斷方法
方法
作用
示例
結果
startswith(prefix)
是否以某子串開頭
"hello".startswith("he")
True
endswith(suffix)
是否以某子串結尾
"world".endswith("ld")
True
isalnum()
是否字母或數字
"abc123".isalnum()
True
isalpha()
是否全為字母
"abc".isalpha()
True
isdigit()
是否全為數字
"123".isdigit()
True
isnumeric()
是否數字字符
"Ⅷ".isnumeric()
True
isdecimal()
是否十進制數字
"12".isdecimal()
True
isspace()
是否全為空白字符
" ".isspace()
True
islower()
是否全小寫
"hello".islower()
True
isupper()
是否全大寫
"HELLO".isupper()
True
istitle()
是否標題化(首字母大寫)
"Hello".istitle()
True
# 密碼強度驗證
password ="Passw0rd!"
has_upper =any(c.isupper()for c in password)
has_lower =any(c.islower()for c in password)
has_digit =any(c.isdigit()for c in password)
has_special =any(not c.isalnum()for c in password)print(f"密碼強度: {has_upper and has_lower and has_digit and has_special}")
四、字符串分割與連接方法(常用)
1. 分割方法
方法
作用
示例
結果
split(sep)
按分隔符分割
"a,b,c".split(",")
['a', 'b', 'c']
rsplit(sep)
從右開始分割
"a,b,c".rsplit(",", 1)
['a,b', 'c']
splitlines()
按行分割
"第一行\n第二行".splitlines()
['第一行', '第二行']
partition(sep)
分成三部分
"hello.world".partition(".")
('hello', '.', 'world')
rpartition(sep)
從右分成三部分
"hello.world.py".rpartition(".")
('hello.world', '.', 'py')
# 解析URL參數
url ="https://example.com?name=John&age=25"
_, params = url.split("?",1)# 分割一次
params_dict =dict(p.split("=")for p in params.split("&"))print(params_dict)# 輸出: {'name': 'John', 'age': '25'}
2. 連接方法
方法
作用
示例
結果
join(iterable)
連接字符串序列
",".join(["a","b","c"])
"a,b,c"
# 路徑拼接
parts =["C:","Users","John","Documents"]
path ="\\".join(parts)# Windows路徑print(path)# 輸出: C:\Users\John\Documents
第65篇:基于大模型的文檔問答系統實現 📚 摘要:本文詳解如何構建一個基于大語言模型(LLM)的文檔問答系統,支持用戶上傳 PDF 或 Word 文檔,并根據其內容進行智能問答。從文檔解析、向量化、存儲到…