How to return all capital letters within string in Panda using Regex

Viewed 55

I am struggling to get the second output which is "None" when I tried to return all capital letters within the statement below

i tired the below

def findUpperCase(string):
    r = re.findall('([A-Z][a-z]+)', string)
    for word in string:
        if r == string:
            r
        return sorted(r)

print( findUpperCase('Hello World Paython.') )
# Output [Hello, World, Paython] 
print( findUpperCase('i am still learning') )
# Output None
1 Answers

Since an empty list will evaluate as False, you can use an if statement and explicitly return None if the list is empty (no matches).

def findUpperCase(string):
    r = re.findall('([A-Z][a-z]+)', string)
    if r:
        return sorted(r)
    return None

print( findUpperCase('Hello World Paython.') )
# Output ['Hello', 'Paython', 'World'] 
print( findUpperCase('i am still learning') )
# Output None
Related