python删除字符串中的字母_在Python中删除字符串中的大写字母
我想寫一個函數(shù)消除一個字符串和2個可選參數(shù)。第一個可選參數(shù)(bad_characters)取一個字母,第三個參數(shù)(case_sensitive)取一個布爾值。該函數(shù)應(yīng)該接受一個字符串s并移除bad_characters的所有實例。如果case_sensitive為true,那么函數(shù)應(yīng)該區(qū)分大小寫。如果錯誤,那么它不需要。這是我迄今為止所擁有的。在Python中刪除字符串中的大寫字母
def eliminate(s,bad_characters = [],case_sensitive = True or False):
''' Takes a string s and returns the string with all bad_characters
removed. If case_sensitive is True, then the function will only
remove uppercase letters if there are uppercase letters in
bad_characters.
String --> String'''
while True:
if case_sensitive = False:
for character in s:
if bad_characters == character:
newlist = s.replace(bad_characters,'')
return newlist
break
我很難搞清楚如何使功能刪除大寫字母,如果需要的話。如果bad_characters是一個列表,元組或字符串,該函數(shù)也應(yīng)該可以工作。
2015-04-06
Brett
+1
'True或FALSE' == TRUE;。這是你的意圖嗎? –
2015-04-06 12:00:27
+0
為什么'case_sensitive'只能刪除大寫字母?這很奇怪。也許把它命名為'uppercase_only'? –
2015-04-06 12:07:26
+0
另外,請在python控制臺中嘗試以下內(nèi)容:''A'=='a''。 –
2015-04-06 12:09:39
總結(jié)
以上是生活随笔為你收集整理的python删除字符串中的字母_在Python中删除字符串中的大写字母的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 黑解的苹果可以买吗(汉典黑字的基本解释)
- 下一篇: python爬虫天气数据_python爬