1

I am a beginner in Python and I want to remove all Capital numbers from the string. I have tried below code but getting None after each function call. What should I return from the function?

def remove_capitals(a_string):
    for latter in a_string:

        if not (ord(latter) in range(65,91)):
            print(latter,end="")
        else:
            continue

print(remove_capitals("A1B2C3D"))
print(remove_capitals("Georgia Institute of Technology"))

I am getting below Output

123None
eorgia nstitute of echnologyNone
6
  • 1
    else: continue is pointless at the end of a loop. Commented Sep 5, 2018 at 11:46
  • 2
    your function is missing the optional return statement so it returns the default None. if the print happens inside the function, remove it from outside and just do remove_capitals("A1B2C3D"). Otherwise, instead of printing inside, return it and print it outside., Commented Sep 5, 2018 at 11:47
  • 1
    Your function already prints(), then returns None. You then print that return value. Commented Sep 5, 2018 at 11:47
  • 123 eorgia nstitute of echnology Commented Sep 5, 2018 at 11:47
  • Either return a single result at the end of your function and not print inside the function, or remove the print() calls around remove_capitals(). Commented Sep 5, 2018 at 11:47

2 Answers 2

2

You can use a generator expression with in str.join to remove all uppercase letters, which are enumerated in string.ascii_uppercase

from string import ascii_uppercase
def remove_capitals(a_string):
    return ''.join(i for i in a_string if i not in ascii_uppercase)

>>> print(remove_capitals("A1B2C3D"))
123
>>> print(remove_capitals("Georgia Institute of Technology"))
eorgia nstitute of echnology
Sign up to request clarification or add additional context in comments.

5 Comments

Much faster: no_caps = dict.fromkeys(range(65, 91)) then return a_string.translate(no_caps).
you could pass a list to join instead of a generator to speed it up. Otherwise, +1
not in ascii_uppercase means that for any lowercase letter you have to loop over every character in ascii_uppercase. Couldn't you just use not char.isupper()?
@DSM: that'd expand the possible characters that are removed, when the input uses codepoints outside of ASCII.
@MartijnPieters: say rather that it wouldn't limit the possible uppercase characters that are removed to the ones the OP hardcoded as "Capital numbers".
1

Use isupper,

def remove_upper_case(x):
        return ''.join(i for i in x if not i.isupper())

Execution:

In [281]: remove_upper_case("Georgia Institute of Technology")
Out[281]: 'eorgia nstitute of echnology'

In [282]: remove_upper_case("A1B2C3D")
Out[282]: '123'

1 Comment

No need for the list expression, a simple generator expression is enough.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.