string interpolation for python
Terry Reedy
tjreedy at udel.edu
Sat Mar 31 06:29:11 EDT 2012
On 3/31/2012 2:22 AM, Yingjie Lan wrote:
> Hi all,
>
> I'd really like to share this idea of string interpolation for formatting.
> Let's start with some code:
>
> >>> name = "Shrek"
> >>> print( "Hi, $name$!")
> Hi, Shrek!
> >>> balls = 30
> >>> print( "We have $balls$ balls.")
> We have 30 balls
You can already do essentially that without adding a special-case string
formatting method to the general methods we already have.
>>> balls = 5
>>> people = 3
>>> 'The {people} people have {balls} balls.'.format(**locals())
'The 3 people have 5 balls.'
--
Terry Jan Reedy
More information about the Python-list
mailing list