Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
George John

Python string template class is used to create a simple template string. Python template strings were first introduced in python 2.4. Python string template is created by passing template strings as argument to its constructor. Where string formatting operators used for the percentage sign for substitutions and the template object uses dollar signs.

Template class provide three methods to create a string from the template −

  • Class string.Template(template) - The constructor takes a single argument, which is the template string.

  • substitute(mapping, **keywords) - Method that substitutes the string values(mapping) for the template string values. Mapping is a dictionary-like object, and its values may be accessed as a dictionary. If the keywords argument is used, it represents placeholders. When both mapping and keywords are used, the keyword takes the precedence. If a placeholder is missing from mapping or keywords, a keyError is thrown.

  • safe_substitute(mapping, **keywords) - Functions similarly to the substitute(). However, if a placeholder is missing from mapping or keywords, the original placeholder is used by default, thus avoiding the KeyError. Also, any occurrence of ‘$’ returns a dollar sign.

Template objects also have one publicly available attribute−

  • Template - It is the object passed to the constructor’s template argument. Although read-only access is not enforced, it is advisable not to change this attribute in your program.

Python Template string examples

from string import Template

t = Template('$when, $who $action $what.')
s= t.substitute(when='In the winter', who='Rajesh', action='drinks', what ='Coffee')
print(s)

#dictionary as substitute argument

d = {"when":"In the winter", "who":"Rajesh", "action":"drinks","what":"Coffee"}
s = t.substitute(**d)
print(s)

output

In the winter, Rajesh drinks Coffee.
In the winter, Rajesh drinks Coffee.

safe_substitute()

from string import Template

t = Template('$when, $who $action $what.')
s= t.safe_substitute(when='In the winter', who='Rajesh', action='drinks', what ='Coffee')
print(s)

Result

In the winter, Rajesh drinks Coffee.

Printing template string

The template attribute in template object returns the template string.

from string import Template

t = Template('$when, $who $action $what.')
print('Template String: ', t.template)

Result

Template String: $when, $who $action $what.

Escaping $ sign

from string import Template

t = Template('$$ is called $name')
s=t.substitute(name='Dollar')
print(s)

Result

$ is called Dollar

${identifier} example

${<identifier>} is equivalent to $<identifier>

It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as ${noun}ification.

from string import Template
t = Template('$noun adjective is ${noun}ing')
s = t.substitute(noun='Test')
print(s)

Result

Test adjective is Testing

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.