python删除字符串中的字母_在Python中删除字符串中的大写字母

我想写一个函数消除一个字符串和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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值