python-3.xfunctionprintingord

How to return the result of print statement in a function call in python?


I am a beginner and I am having a problem returning a value in python. The Question is as follows: Write a function called get_capitals. get_capitals should accept one parameter, a string. It should return a string containing only the capital letters from the original string: no lower-case letters, numbers, punctuation marks, or spaces. In short, the function should return the string which will only contain capital letters. My code is:

def get_capitals(a_string):
    for x in a_string:
        ordinal_number=ord(x)
        if ordinal_number>=60 and ordinal_number<=90:
           print(x,end=""))
           # print(ordinal_number)

Using function call

print(get_capitals("CS1301"))

call with the above call the above code i am able to print the result i desire but it returns None. which I am trying to avoid. Can someone tell me how can I return a result of the print function?


Solution

  • Use list comprehension and join

    def get_capitals(a_string):
        return ''.join(c for c in a_string if c.isupper())