On this page
string.Template
Prev: string.Formatter | Up: string
Rel: string.capwords, string.Formatter
Ref: docs.python.org — string.Template
Tags: #public #python3 #stdlib #string
Import
import string
from string import Template
Signature & Overview
string.Template(template)
A string class for supporting $-substitutions.
Methods & Attributes
Template.get_identifiers(self)—Template.get_identifiersmethod/property.Template.is_valid(self)—Template.is_validmethod/property.Template.safe_substitute(self, mapping={}, /, **kws)—Template.safe_substitutemethod/property.Template.substitute(self, mapping={}, /, **kws)—Template.substitutemethod/property.
Usage
import string
tpl = string.Template('Hello $user, balance is ${amount} USD')
assert tpl.safe_substitute(user='Ada') == 'Hello Ada, balance is ${amount} USD'
Gotchas
- Safer than
str.format()for user-supplied templates: Cannot access object attributes ({0.__class__}) or indexing.
REPL Inspect
from string import Template
help(Template)
Linked from
Related notes
- tuts/python3/stdlib_module/string/capwords Linked here; links to this page; shared tags: #stdlib, #string
- tuts/python3/stdlib_module/string/Formatter Linked here; links to this page; shared tags: #stdlib, #string
- tuts/python3/stdlib_module/string Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib/string shared tags: #stdlib, #string
- tuts/c/stdlib/string/memchr shared tags: #stdlib, #string
- tuts/c/stdlib/string/memcmp shared tags: #stdlib, #string
- tuts/c/stdlib/string/memcpy shared tags: #stdlib, #string
- tuts/c/stdlib/string/memmove shared tags: #stdlib, #string
- tuts/c/stdlib/string/memset shared tags: #stdlib, #string
- tuts/c/stdlib/string/strcat shared tags: #stdlib, #string