You could use regular expressions.
Define a regex that matches the first word of a sentence:
import re
p = re.compile(r'(?<=[\.\?!]\s)(\w+)')
This regex contains a positive lookbehind assertion (?<=...)
which matches either a .
, ?
or !
, followed by a whitespace character \s
. This is followed by a group that matches one or more alphanumeric characters \w+
. In effect, matching the next word after the end of a sentence.
You can define a function that will capitalise regex match objects, and feed this function to sub()
:
def cap(match):
return(match.group().capitalize())
p.sub(cap, 'Your text here. this is fun! yay.')
You might want to do the same for another regex that matches the word at the beginning of a string:
p2 = re.compile(r'^\w+')
Or make the original regex even harder to read, by combining them:
p = re.compile(r'((?<=[\.\?!]\s)(\w+)|(^\w+))')