String Template Class
In the String module, Template Class allows us to create simplified syntax for output specification. The format uses placeholder names formed by $ with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing $$ creates a single escaped $:
Example: Formatting string using Template Class
Python3
from string import Template n1 = 'Hello' n2 = 'w3wiki' # made a template which we used to # pass two variable so n3 and n4 # formal and n1 and n2 actual n = Template( '$n3 ! This is $n4.' ) # and pass the parameters into the template string. print (n.substitute(n3 = n1, n4 = n2)) |
Hello ! This is w3wiki.
Note: To know more about the String Template class, refer to String Template Class in Python
Python String InterpolationPython String Interpolation
String Interpolation is the process of substituting values of variables into placeholders in a string. Let’s consider an example to understand it better, suppose you want to change the value of the string every time you print the string like you want to print “hello <name> welcome to geeks for geeks” where the <name> is the placeholder for the name of the user. Instead of creating a new string every time, string interpolation in Python can help you to change the placeholder with the name of the user dynamically.
Contact Us