There are two ways to change the formatting, first is using operators to replicate the steps you would do by hand.
That should look obvious - move to the beginning of the line, move and select to the next word, then apply a style to the selected text.
The other way is to adjust the data that stores the formatting. A font object uses a TextCurve to store it's object data, the
body_format properties are what you are interested in.
body is the text string and
body_format is a list of TextCharacterFormat which includes
use_underline for each character in the body.
So to underline the first 6 characters of the active object -
for c in range(6):
bpy.context.object.data.body_format[c].use_underline = True