def template_substitute ( text , * * kwargs ) : for name , value in kwargs . items ( ) : placeholder_pattern = "{%s}" % name if placeholder_pattern in text : text = text . replace ( placeholder_pattern , value ) return text
python code to replace data in a template text
Replace placeholders in text by using the data mapping . Other placeholders that is not represented by data is left untouched .
def template_substitute(text, **kwargs):
    """
    Replace placeholders in text by using the data mapping.
    Other placeholders that is not represented by data is left untouched.

    :param text:   Text to search and replace placeholders.
    :param data:   Data mapping/dict for placeholder key and values.
    :return: Potentially modified text with replaced placeholders.
    """
    for name, value in kwargs.items():
        placeholder_pattern = "{%s}" % name
        if placeholder_pattern in text:
            text = text.replace(placeholder_pattern, value)
    return text