Python String Template Class
By Lenin Mishra
Python String Template
The String Template class in Python is used to create a template string using a $
sign. These fields can be replaced later on to create a string object.
This class has 2 key methods:
===============================
1. substitute(mapping, **kwds):
===============================
This method performs substitutions using a
"dictionary like" key-value based mapping objects.
If keys are missing, it returns a KeyError.
====================================
2. safe_substitute(mapping, **kwds):
====================================
Similar behavior as above, but it doesn�t throw a
KeyError if a key is missing. It just returns the
placeholder in the result string.
Let’s look at a few examples.
String Template substitute() method
Example 1
Code
from string import Template
t = Template('$name writes blogs on $language')
s = t.substitute(name='Pylenin', language='Python')
print(s)
# dictionary as substitute argument
d = {'name': 'Pylenin', 'language': 'Python'}
s = t.substitute(**d)
print(s)
Output
Pylenin writes blogs on Python
Pylenin writes blogs on Python
Example 2
Code
from string import Template
# Students and their respective heights
Student = [('Lenin', 190), ('Darshana', 180), ('Chinmayee', 150)]
# Create a basic structure
# to print the name and height
t = Template('Hi $name, your height is $height cm')
for i in Student:
print(t.substitute(name=i[0], height=i[1]))
Output
Hi Lenin, your height is 190 cm
Hi Darshana, your height is 180 cm
Hi Chinmayee, your height is 150 cm
Let’s look at the possible errors in String Template substitute() method.
- If a key is missing from your dictionary like object, it will throw a
KeyError
. - Any other appearance of
$
in the string will result in aValueError
.
Code - Example 1
from string import Template
t = Template('$name writes blogs on $language')
d = {'name': 'Pylenin'}
s = t.substitute(**d)
print(s)
Output - Example 1
KeyError: 'language'
Code - Example 2
from string import Template
t = Template('Give $name $100')
s=t.substitute({"name":"Pylenin"})
print(s)
Output - Example 2
ValueError: Invalid placeholder in string: line 1, col 12
You can get rid of the above 2 errors using the following:-
KeyError
- Use safe_substitute() methodValueError
- Use safe_substitute() method or Escape the$
sign using$$
.
String Template safe_substitute() method
If placeholders are missing from mapping and kwds, instead of raising a KeyError exception, the original placeholder will appear in the resulting string.
Code
from string import Template
t = Template('$name writes blogs on $language')
d = {'name': 'Pylenin'}
s = t.safe_substitute(**d)
print(s)
Output
Pylenin writes blogs on $language
It can also escape any other appearances of the $
sign.
Code
from string import Template
t = Template('Give $name $100')
s=t.safe_substitute({"name":"Pylenin"})
print(s)
Output
Give Pylenin $100
Escaping $
sign with $$
Another way to escape any other appearances of the $
sign, is to use $$
sign.
If you use $$
, you can safely use the substitute()
method.
Code
from string import Template
t = Template('Give $name $$100')
s=t.substitute({"name":"Pylenin"})
print(s)
Output
Give Pylenin $100
Python offers 3 other ways of String Formatting. Check out the definite guide to Python String Formatting
Related Articles
- f-strings in Python
- Python String format() method
- % operator - Python String formatting
- How to create a string in Python?
- How to access characters in a Python string?
- How to replace characters in a string in Python?
- How to concatenate strings in Python?
- How to iterate through a string in Python?
- Check if a Substring is Present in a Given String in Python
- Escape sequences in Python String