Parameterize formatting string

David wizzardx at gmail.com
Fri Sep 21 14:16:29 EDT 2007


On 9/21/07, cyril giraudon <cyril.giraudon at gmail.com> wrote:
> Hello,
>
> I 'd like to know if a std::setw() equivalent function exists in
> python ?
>
> i thought of something like :
>
> a = 16
> "%ai" % 12
>
> But it is not correct.
>
> Any Idea ?

("%i" % 12).rjust(a)

Or, more ugly:

"%%%di" % a % 12

The first % (after quotes) builds this string: "%16i".
Then the second % operation converts that string to '              12'

See also: http://docs.python.org/tut/node9.html



More information about the Python-list mailing list