Parameterize formatting string

Jean-Paul Calderone exarkun at divmod.com
Fri Sep 21 14:23:54 EDT 2007


On Fri, 21 Sep 2007 20:16:29 +0200, David <wizzardx at gmail.com> wrote:
>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'
>

Or '%*i' % (a, 12)

Jean-Paul



More information about the Python-list mailing list