我想写一个函数消除一个字符串和2个可选参数。第一个可选参数(bad_characters)取一个字母,第三个参数(case_sensitive)取一个布尔值。该函数应该接受一个字符串s并移除bad_characters的所有实例。如果case_sensitive为true,那么函数应该区分大小写。如果错误,那么它不需要。这是我迄今为止所拥有的。在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是一个列表,元组或字符串,该函数也应该可以工作。
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控制台中尝试以下内容:''A'=='a''。 –
2015-04-06 12:09:39