Intor

Here, we are not talk about THE template as we have used to talk about the tempale engine such as jinjia2.
but, we are talking about string templete
Commonly we write like this print('balalabala %d' % v) it makes v instead of %d
or in new style or THE F word whateve you want call it. print(f'balabala {v}')

Templates provide simpler string substitutions as described in PEP 292. Instead of the normal “%”-based substitutions. As we know, a string when we want to contert them to some format way.

Talk is cheap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 2018-07-27 22:46:42
# @Author : Simon (simon.xie@codewalker.meg)
# @Link : http://www.codewalker.me
# @Version : 1.0.0

from string import Template

class MyTemplate(Template):
delimiter = '#'


def main():
cart = []
cart.append(dict(
item='Coke',
price=8,
qty=3
))
cart.append(dict(
item='Cake',
price=4,
qty=4
))
cart.append(dict(
item="Apple",
price=1,
qty=5
))
t = Template("$qty x $item per $price")
total = 0
for item in cart:
print(t.substitute(item))
total += item['price'] * item['qty']
print(f'Total: {total}')
if __name__ == "__main__":
main()

Read it & comprehen it & love it

EOF.