How can I format the string according to its length at runtime?

Carel Fellinger cfelling at iae.nl
Wed Jun 20 02:29:35 CEST 2001


ed_tsang at yahoo.com wrote:
> How can I format the string but according to the length it has?
...
> l = 6 - len(str(values))
> printwrite(trcfile, "Rx value = %-5s (0x%-6X)-  OK\n"%(str(values), 
> values),tag )
...
> My problem is when it displayed in hexadecimal, there are extra 
> spacing because it of the statement 0x%-6X when the lengeth of values 
> is 3. But I will no know in advance what is the length of values, it 
> can reach 6. How can I dynmically alter the number of spaces in this 
> case?

I'm not sure I understand your problem, wouldn't simply leaving out
the field widths work? like:

>>> value = 30
>>> "Rx value = %d (0x%X)-  OK\n" % (value, value) 
'Rx value = 30 (0x1E)-  OK\n'




-- 
groetjes, carel



More information about the Python-list mailing list