简体   繁体   中英

Checking Sentence Case in String

I wonder how to check for sentence case in string. I know about isupper() method that checks for any uppercase character. But what about sentence case? For example:


def check_string(string):
    <...>
    return result

check_string('This is a proper sentence case.')
True

check_string('THIS IS UPPER CASE')
False

check_string('This string is Clean')
False

A quick trick for this would be using capitalize() and check if it is equal to the original input:

def check_string(my_string):
    cap_string = my_string.capitalize()
    if (cap_string == my_string):
        return True
    return False
def check_string(string): sentences = string.split(".") for sentence in sentences: sentence = sentence.strip() if sentence and (not sentence[0].isupper() or any(filter(lambda x: x.isupper(), sentence[1:]))): return False return True

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