Python uses the String Template class
Using the String Template Class in Python
Python’s standard library includes a string module that provides functionality for performing various string operations.
The Template class in the string module provides an alternative method for dynamically formatting strings. One advantage of the Template class is the ability to customize formatting rules.
The Template implementation uses regular expressions to match common patterns for valid template strings. A valid template string, or placeholder, consists of two parts: a leading $ sign followed by a valid Python identifier.
You need to create an object of the Template class and pass the template string as a constructor argument.
Then call the substitute() method of the Template class. This replaces the provided value as an argument in the template string’s place.
Example
from string import Template
temp_str = "My name is <span class="katex math inline">name and I am</span>age years old"
tempobj = Template(temp_str)
ret = tempobj.substitute(name='Rajesh', age=23)
print (ret)
This will produce the following output −
My name is Rajesh and I am 23 years old
We can also extract key-value pairs from dictionaries to replace values.
from string import Template
student = {'name':'Rajesh', 'age':23}
temp_str = "My name is <span class="katex math inline">name and I am</span>age years old"
tempobj = Template(temp_str)
ret = tempobj.substitute(**student)
print(ret)
It will produce the following output −
My name is Rajesh and I am 23 years old