py3k - format specifier for strings

"Martin v. Löwis" martin at v.loewis.de
Mon Sep 3 23:46:35 EDT 2007


>>>> a = "{0}".format(5.66)
>>>> a
> '5.66'
> 
> There are more options in PEP 3101 (fill, alignment, etc.), but I'm having
> trouble implementing them.

It would be good if you could describe these troubles in more detail.
What have you been trying, what happened, and what did you expect to
happen instead?

> Any help in the way of examples would be appreciated.

>From test_unicode.py:

"My name is {0}".format('Fred')
"My name is {0[name]}".format(dict(name='Fred')
"My name is {0} :-{{}}".format('Fred')

d = datetime.date(2007, 8, 18)
"The year is {0.year}".format(d)

'{0:.3s}'.format('abc')
'{0:.0s}'.format('abcdef')
'{0:3.3s}'.format('abc')
'{0:3.2s}'.format('abc')
'{0:x<0s}'.format('result')
'{0:x<7s}'.format('result')
'{0:^10s}'.format('result')
'{0!s}'.format('Hello')
'{0!s:15s}'.format('Hello')
'{0!r:}'.format('Hello')

Regards,
Martin



More information about the Python-list mailing list