Change language

String class template in Python

| |

Below is a simple example.

 

# Simple Python templaye example

from string import Template

 
# Create a template that has a placeholder for the x value

t = Template ( ’x is $ x’ )

 
# Substitute the x value in the template above

print (t.substitute ({ ’x’ : 1 } ))

Output:

 x is 1 

Below is another example in which we import the names and grades of the students from the list and print them using the template.

# Python program that demonstrates how the inline template works

from string import Template

  
# Student list stores the names and grades of three students

Student = [( ’Ram’ , 90 ) , ( ’Ankit’ , 78 < / code> ), ( ’Bob’ , 92 )]

 
# We create a basic structure for printing the name and
# student grades.

t = Template ( ’ Hi $ name, you have got $ marks marks’ )

 

for i in Student:

print (t.substitute (name = i [ 0 ], marks  = i [ 1 ]))

Output:

 Hi Ram, you’re 90 ratings Hi Ankit, you have 78 ratings Hello Bob, you have 92 ratings 

The substitute () method raises a KeyError when a placeholder is not specified in a dictionary or keyword argument. For mail merge-style applications, user supplied data may not be complete and safe_substitute () may be more appropriate — it will leave placeholders unchanged if no data is available:

Another template application separates the program logic from the details of multiple output formats. This allows you to replace custom templates for XML files, text reports, and HTML web reports.

It should be noted that there are other ways to print formatted output as well, like% D for an integer,% F for a float, and etc. (see this for details)

Link: https://docs.python.org/3.3/tutorial/stdlib2.html

This article is contributed by Siddharth Lalwani. If you like Python.Engineering and would like to contribute, you can also write an article and mail it to [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you’d like to share more information on the topic under discussion

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically