Python Template & Concept of Do while loop in Python.

Lalita Rajpoot
3 min readJan 16, 2022

What Is Template ?

In python, template is a class of String module, allows for data to change without having to edit the application. It can be modified with subclasses. Templates provide simpler string substitutions. substitution is “$“-based substitutions, rather than “%“-based substitutions.

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.

How It Works ?

  • Template class takes a string as a template, within the string use placeholder variable name with preceding the Dollar($) sign to depict there is the placeholder.
  • Then substitute value into the template with substitute() method using a dictionary. Where key is in the dictionary match with placeholder name. The returned string is the template with all the values rather than the placeholder
  • It should also be noted that, the placeholder variable name should also be follow the same naming convention as variable naming in python.

Substitution Rules For Python

For performing substitution we should have to follow the following rules –

  • “$$“ is an escape ; it is replaced with a single “$“.
  • “$identifier“ names a substitution placeholder matching a mapping key of “identifier”. By default, “identifier” must spell a Python identifier. The first non-identifier character after the “$” character terminates this placeholder specification.
  • “${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”.

Implement Template:-

from string import Templatemy_template = Template('My Name is $x')print (my_template.substitute({'x' : "Lalita Rajpoot"}))
  • First of all import Template class from string module.
  • Then create a template that has placeholder for value of x.
  • And finally Substitute value of x in above template.

Why Use Custom Templates ?

  • It saves typing and reduces code size.
  • Easy to implement custom templates from command line.
  • For eg. — MyPhoto_$n$ext = MyPhoto_0.jpg, MyPhoto_1.jpg etc.
  • Custom templates are extremely useful for webpages, as a webpage generally always follows the same template but with different data.

Do while loop:-

Do While Loop conditional statement is used for an exit level control flow of code implementation that ensures the code block is executed at least once before the control reaches the while condition. In Python, there is no dedicated do while conditional loop statement, and so this function is achieved by creating a logical code from the while loop, if statement, break and continue conditional statements. When the logic of the program is done correctly, depending on the requirement provided, the Do While loop can be imitated perfectly.

Syntax of do-while:-

do
{
Statement(statement)

There are two scenarios in which a loop terminates:

  • The loop condition is no longer true/false (depending on the type of loop).
  • A break statement is executed from within the code in the loop body.

Code :-

i = 1while True:
print(i)
i = i + 1
if(i > 3):
break

Thanks for reading :)

--

--