牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Pythonのstringにある文字列テンプレートやってみた

Python チュートリアル 11.2 文字列テンプレート』にあったstringモジュールのTemplateクラスを使ってみた。

from string import Template

d = {"Guido van Rossum":"Python", "Larry Wall":"Perl", "Matz":"Ruby", "James Gosling":"Java"}
t = Template('$auth is the author of the $prog programming language.')
print "\n".join([t.substitute(auth=a, prog=p) for a,p in d.items()])

実行結果

Larry Wall is the author of the Perl programming language.
James Gosling is the author of the Java programming language.
Guido van Rossum is the author of the Python programming language.
Matz is the author of the Ruby programming language.


あれ?テンプレート使わなくてもいける気がする?
これ便利なのかしらん(´・ω・`)