The Blender documentation explains how to apply multiple materials to various selections in Text:
(source: blender.org)
For example, to create Fig. Red Green Blue. you would need to create three separate materials and three separate material indices. Each word would be assigned a Material index by selecting the characters for each word and clicking the Assign button. Fig. Red Green Blue. is still one single Text object.
When accessing the text using a Python script, we can see its contents as a string through the following command:
object.data.body
However, it is not clear to me how we could mark substrings and assign materials to them. Do we need to macro the operations (select a range, assign material, repeat) as if we were doing this through the UI? Is there a more streamlined API?
Is this achievable through a script at all?