4.8. Capitalize Sentences ¶
Write a function named capitalize_sentences which takes a string including multiple sentences and returns a string where the first letter of each sentence in the original string is converted to uppercase to correct the typos.
A sentence may end with the following characters: '.'
, '?'
,
'!'
. Your function should not interfere with other parts of the
string.
You may assume that there will be a single blank character between each sentence. You must preserve this blank character in the recovered string. There will be no blank characters after the last sentence.
Sample I/O:
>>> capitalize_sentences('lorem. ipsum? dolor sit amet, consectetur! adipiscing elit.')
'Lorem. Ipsum? Dolor sit amet, consectetur! Adipiscing elit.'
>>> capitalize_sentences('string methods are really useful in Python! you need to know them to succeed in CENG240.')
'String methods are really useful in Python! You need to know them to succeed in CENG240.'
def capitalize_sentences(string):
def upper_initial(string):
return string[0].upper() + string[1:] if string else string
def split_by_and_capitalize(string, delimiter):
sentences = [sentence.lstrip() for sentence in string.split(delimiter)]
return (delimiter + ' ').join(
[upper_initial(sentence) for sentence in sentences])
delimiters = ['.', '?', '!']
for delimiter in delimiters:
string = split_by_and_capitalize(string, delimiter)
return string.rstrip() # discard trailing whitespace