當前位置:首頁 » 編程語言 » python去除字母

python去除字母

發布時間: 2023-03-03 08:50:48

1. python字元串如何去掉英文字母以外的字元

#內置函數ord可以返回字元對應的編碼
#鍵盤上能找到的字元,對應的編碼為32-126
#列表推導式過濾掉其他字元,join重新拼接字元串
deftest(wenben):return''.join([xforxinwenbenif32<=ord(x)<=126])
print(test('Li漢字與中文標點、,。都不會顯示feisshort,youne和諧edPython.'))

2. python字元串如何去掉英文字母以外的字元

可以利用正則表達式來去除

既然說到了字元串的操作,那麼就目前而言是沒有別的方法會比正則表達式更加方便的:

正則表達式中代表非字母的寫法如下:

[^a-zA-Z]

#code:

3. python怎麼不用strip()就去掉首末的字母啊

#-*-coding:utf-8-*-

defstrip(strs,fits):
strs=list(strs)
foridxinrange(0,len(strs),1):
ifstrs[idx]infits:
strs[idx]=None
continue
break
foridxinrange(len(strs)-1,-1,-1):
ifstrs[idx]infits:
strs[idx]=None
continue
break
return''.join([cforcinstrsifcisnotNone])

printstrip("abcdeddaab","ab")

如果解決了您的問題請採納!
如果未解決請繼續追問

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:585
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:881
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:574
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:761
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:676
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1005
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:249
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:108
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:798
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:705