[Tutor] [Re:] I need help with the following question

Steven D'Aprano steve at pearwood.info
Wed Sep 11 13:54:03 CEST 2013


On Wed, Sep 11, 2013 at 11:15:26AM +0100, Oscar Benjamin wrote:

> There is also the .format method. This was initially intended to
> replace % formatting but it was ultimately decided that removing %
> formatting was not necessary. Consequently there are now two ways of
> doing advanced string formatting in Python.

Three ways.

People forget the string.Template class.

py> import string
py> template = string.Template("Hello $name")
py> template.substitute(name="Oscar")
'Hello Oscar'
py> template.safe_substitute(nmae="Oscar")
'Hello $name'



-- 
Steven


More information about the Tutor mailing list