- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
String Template Class in Python?
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
- Related Articles
- String Template Class in C#
- Template matching using OpenCV in Python
- String class in C#
- String class in Java
- Program to implement run length string decoding iterator class in Python
- Template Metaprogramming in C++
- Template Specialization in C++
- is_final template in C++
- is_fundamental Template in C++
- is_pod template in C++
- is_pointer Template in C++
- is_signed template in C++
- is_unsigned template in C++
- is_void template in C++
- is_standard_layout template in C++
