Home > Blockchain >  Checking Sentence Case in String
Checking Sentence Case in String

Time:09-16

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

CodePudding user response:

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

CodePudding user response:

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
  • Related