简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM