PyDocs

Nav

Text Processing Services

Template

string

class (source)
  • Template(template)
A string class for supporting $-substitutions.
Members of Template

g

get_identifiers
Returns a list of the valid identifiers in the template, in the order they first appear, ignoring any invalid identifiers.

i

is_valid
Returns False if the template has invalid placeholders that will cause substitute() to raise ValueError.

s

safe_substitute
Like substitute(), except that if placeholders are missing from mapping and kwds, instead of raising a KeyError exception, the original placeholder will appear in the resulting string intact.
substitute
Performs the template substitution, returning a new string.

t

template
This is the object passed to the constructor’s template argument.
1 Example
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11
>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'
from the Python docs
See Also
No see-alsos for string/Template
0 Notes
No notes for Template