Templates concept in PYTHON

What is Template ?

Template is a class of String module.

It allows for data to change without having to edit the application. It can be modified with subclasses. Templates provide simpler string substitutions . Templates substitution is “$“-based substitutions

How do I use a template in Python?

To use the Python Template class in our code, we need to:

  • Import Template from the string module.
  • Create a valid template string.
  • Instantiate Template using the template string as an argument.
  • Perform the substitution using a substitution method.

How it works ?

  • Takes a string as a template with placeholder variables to be over written.
  • You then Substitute the values in with a dictionary.The key being the placeholder name
  • Placeholder names follow the same rules as variables naming in python


example program in python using templates

program in python on using templates

OUTPUT:

No alt text provided for this image


Template Errors

  • No placeholder match,will result in a KeyError in which the template wasn't given a value for a placeholder

No alt text provided for this image

here we didn't provided the value for placeholder error,so we get key error

No alt text provided for this image


  • Bad placeholder,will result in a ValueError.The placeholder started with an invalid character(placeholder when started with number)

No alt text provided for this image

here,the placeholder price is started with number ,so we get value error

No alt text provided for this image


Safe_substitute()

  • Templates can handle these errors and give us back a string still if we use the method safe_substitute()
  • You also get the placeholders in the returned string if there is an error with it.

No alt text provided for this image
No alt text provided for this image





To view or add a comment, sign in

More articles by Anudeepthi Kolagani

  • To launch Docker in Ec2 Instance and installing Webserver on Docker

    To Launch An Instance in AWS :: Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the…

    2 Comments
  • To launch a redhat ec2 instance in AWS To launch webserver

    To Launch An Instance in AWS :: Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the…

  • LAMBDA Function In AWS

    ▪ AWS Lambda is a serverless compute service that runs your code in response to events ▪ These events may include…

    2 Comments
  • Quantum Computing

    Quantum Computer: ✳ Quantum Computer had the potential to simulate things a classical computer could not feasibly do ✳…

    6 Comments
  • How To Read The Data From "RAM"

    What is RAM ? Random-access memory (RAM) is a computer’s short-term memory. RAM is short for “random access memory” and…

  • FUNNY COMMANDS IN LINUX

    ➡️ cowsay : - An ASCII cow in the terminal will say whatever you want. The cowsay command draws out little animals…

  • DATE COMMAND

    date - print or set the system date and time SYNOPSIS : - date [OPTION]..

  • espeak-ng

    espeak-ng : - A multi-lingual software speech synthesizer SYNOPSIS : - espeak-ng [options] [words] DESCRIPTION : -…

  • ZENITY

    NAME : - zenity - display GTK+ dialogs SYNOPSIS : - zenity [options] DESCRIPTION : - zenity is a program that will…

Explore content categories