0

trying to make a simple function that'll take a phrase, a letter, and then output the original phrase with that letter removed. I can do the simple version but seem to get my wires crossed when I try to cover both lowercase and uppercase. ( it works fine if i do just 'if i not in aChar)

i.e. If i input 'Board of Trade' and my letter to extract is 'O', i want both the uppercase and the lowercase removed. I'm a beginner so any general tips regarding my code would be much appreciated as well.

Here's my script:

def removal(statement,aChar):
   newstring = ''
   lowercase = aChar.lower()
   uppercase = aChar.upper()
   for i in statement:
       if i not in aChar or lowercase or uppercase:
             newstring = newstring+i
   print(newstring)
removal('Board of Trade', 'O')    
SpicyClubSauce
  • 4,076
  • 13
  • 37
  • 62
  • Why not `return string.replace(letter, '')`? – Malik Brahimi Apr 06 '15 at 20:44
  • 1
    `or lowercase or uppercase` this doesnt do what you think it does – Tim Apr 06 '15 at 20:44
  • possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – Tim Apr 06 '15 at 20:45

6 Answers6

3
def removal(statement,aChar):
   newstring = ''
   # look at each character in your statement
   for i in statement:
       # convert everything to lowercase and make sure they don't match
       if i.lower() != aChar.lower():
           newstring = newstring + i
   return newstring
John Dorian
  • 1,884
  • 1
  • 19
  • 29
1

.replace() would make your function slightly more readable.

There is also a mistake with this: i not in aChar or lowercase or uppercase The equivalent of what you wrote is (i not in aChar) or True or True.. which is always true.

def removal(statement,aChar):

   lowercase = aChar.lower()
   uppercase = aChar.upper()

   newstring = statement.replace(lowercase, '').replace(uppercase, '')

   print(newstring)

removal('Board of OTrade', 'O')
user
  • 5,370
  • 8
  • 47
  • 75
0

Change your conditional to test if there is an element in an iterable:

if i not in (aChar, lowercase, uppercase): # you really don't need aChar
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • thanks! curious, in what case would I use the format that I used? – SpicyClubSauce Apr 06 '15 at 20:48
  • Never. As I said in the comments, `or lowercase or uppercase` doesnt do what you would think it does – Tim Apr 06 '15 at 20:49
  • First off, is it working properly? Second, `or` is a logical operator used to compare booleans. `lowercase or uppercase` will always be true because the variables exist. – Malik Brahimi Apr 06 '15 at 20:50
  • @MalikBrahimi don't worry I wasn't being negative about your answer, I was commenting on Spicy's question `in what case would I use the format that I used?` – Tim Apr 06 '15 at 20:55
0

What you could do is convert the capital O to a lowercase O. Similar to this:

def removal(statement,aChar):
   newstring = ''
   for i.lower() in statement:
       if i != aChar.lower()
         newstring += i
   print(newstring)
Bagel
  • 82
  • 11
0

Here is a simple function that replaces the given character with an empty character. It replaces both upper and lower cases of that character. If you just want to replace the character given blindly, use the second answer:

import string

def myfunc(mystr, mychar):
    return (mystr.replace(mychar.lower(), "")).replace(mychar.upper(), "")

myfunc("This is test sentence", "t")

Second answer(use the character given blindly):

def myfunc(mystr, mychar):
    return mystr.replace(mychar, "")
gixxer
  • 814
  • 1
  • 10
  • 25
0

This can be done in one line with str.translate():

def delchar_ci(s, ch):
    return s.translate(None, ch.lower() + ch.upper())

s = 'Board Of Trade'
print delchar_ci(s, 'o') # 'Bard f Trade'
print delchar_ci(s, 'o') == delchar_ci(s, 'O') # True
Shashank
  • 13,713
  • 5
  • 37
  • 63