![](/img/trans.png)
[英]How to remove all but the 26 letters, and . , ( ) ' " ? ! from a string in Python?
[英]Remove letters from string in Python
我需要定義一個帶有兩個參數(s1, s2)
的函數。 此函數應刪除s1
的s2
字母。 例如,從"motorcycle"
刪除字母"motor"
"motorcycle"
應該會產生"cycle"
。 我知道如何處理一個字符串的特定字母,而不是兩個字符串。
做s2.replace(s1, '')
>>> a = 'motor'
>>> a.replace('moto', '')
'r'
Python 中的字符串是不可變的,因此要更改執行的值 -
>>> a =a.replace('moto', '')
專門針對您的問題
def replace_letters(x,y):
return x.replace(y,'')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.