输入一个字符串 str,再输入要删除字符 c,大小写不区分,将字符串 str 中出现的所有字符 c 删除。提示:去掉两端的空格。
输入格式:
在第一行中输入一行字符 在第二行输入待删除的字符
输出格式:
在一行中输出删除后的字符串
实现
#!/usr/bin/env python3 #-*- coding:utf-8 -*-
from functools import reduce
def method_1():
try:
#Transforming string to list type directly,it will include the char of ' ', but it isn't a trouble for this question's context.
s=list(input())
deleteChar=input().strip()
print("result",reduce(lambda x,y:x+y,map(lambda x:'' if x.upper().__eq__(deleteChar.upper()) else x,s)).strip(),sep=":")
except Exception as e:
print("inputing error!",e,sep="\n")
def method_2():
s=input()
dec=input().strip()
s=s.replace(dec.upper(),'')
s=s.replace(dec.lower(),'')
print("result:",s.strip())
def method_3():
i=0
s=list(input())
dec=input().strip()
while i<len(s):
if s[i].__eq__(dec.upper()) or s[i].__eq__(dec.lower()):
s.remove(s[i])
i-=1
i+=1
print("result:",reduce(lambda x,y:x+y,s).strip())
#-------
#method_1()
#method_2()
method_3()
输出
287343jhghjhgj1!hkjd!!!
!
result: 287343jhghjhgj1hkjd
Q.E.D.