一串字符串由几句组成,将每句开头字母大写

按照下面的图,新的句子开头第一个字母大写。当结尾是“ ." , "!" , "?"的时候

img


re.sub('^\w|[.?!]\s*\w', lambda i: i.group().upper(), text)
def capitalize_beginning_of_sentences(text):
    import re
    txts = re.findall('[A-Z a-z]+[.?!]', text)
    for i in range(len(txts)):
        temp = txts[i].strip().split(' ')
        temp[0] = temp[0].strip()
        temp[0] = temp[0].capitalize()
        txts[i] = ' '.join(temp)
        print(txts)

    return ' '.join(txts)

text = "hello class. good luck with the exam ! "
print(capitalize_beginning_of_sentences(text))

img

有帮助请采纳,有问题继续交流,你的采纳是对我回答的最大的肯定和动力