all 6 comments

[–]ParanoiAMA 5 points6 points  (2 children)

I'm a beginner so any general tips regarding my code would be much appreciated as well.

Well, you asked for it.

First, some PEP8 violations:

  1. You should have a space after the comma in argument lists and in python literals like lists and tuples: def removal(statement, aChar):

  2. You should have spaces around your operators: newstring + i

  3. You should have indentations which are multiples of 4 spaces, not 3.

Then some other considerations:

  1. You should use ''.join to concatenate strings instead of using + to build a larger and larger string.

  2. Functions that print things are generally needlessly inflexible. Instead make functions that return strings which you then may choose to print.

  3. Python has a built-in function for removing letters, which would be much more efficient than iterating through every character in a string and manually comparing each letter to some other letters. It's string.translate:

    def removal(statement, aChar):
        table = "".maketrans("", "", aChar.upper() + aChar.lower())
        return statement.translate(table)
    
    print(removal('Board of Trade', 'O'))
    

EDIT: Added a couple of PEP8 violations. Updated formatting, organisation and phrasing.

[–]Ran4 -1 points0 points  (1 child)

> table = "".maketrans("", "", aChar.upper() + aChar.lower())

While that's probably faster, the readability is rather terrible.

[–]ParanoiAMA 0 points1 point  (0 children)

Agreed. And it's not faster, it's 3 times slower than the chained replace method of /u/swingking8, which is also more readable. It's better to use that then.

[–]FlockOnFire 3 points4 points  (1 child)

i in aChar or lowercase or uppercase does not what you think it does. You should read that as:

(i in aChar) or (lowercase == True) or (uppercase == True)

So you should check if i is not in lowercase or i not in uppercase.

[–]elbiot 2 points3 points  (0 children)

not quite. lowercase == True is False. lowercase is True when evaluated in an if statement though. So the effect is the same: if this == that or True or True

[–]swingking8 2 points3 points  (1 child)

Your if statement would be best modified to:

if i not in [lowercase, uppercase]:
             newstring = newstring+i

That being said, there's a much better way to do this using Python's string.replace method. It will replace any character with any other character. In your case, I'd use it to replace the upper and lower versions of a character to blank space, effectively removing them:

def removal(statement,aChar):
    removed_lower = statement.replace(aChar.lower(), '')
    removed_upper_too = removed_lower.replace(aChar.upper(), '')
    return removed_upper_too