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

Emile van Sebille emile at fenx.com
Tue Jun 19 19:02:36 EDT 2001


Does this do what you're looking for?

>>> print "(0x%X)" % 123
(0x7B)
>>> print "(0x%-6X)" % 123
(0x7B    )

HTH

--

Emile van Sebille
emile at fenx.com

---------
<ed_tsang at yahoo.com> wrote in message
news:mailman.992971110.11976.python-list at python.org...
> How can I format the string but according to the length it has?
>
> I have the following code:
>
> l = 6 - len(str(values))
> printwrite(trcfile, "Rx value = %-5s (0x%-6X)-  OK\n"%(str(values),
> values),tag )
>
> Currently I displays:
>
> Check       : TPreason = 120 (0x78)                   Rx value =
> 120   (0x78    )-  OK
>
> function printwrite is just a wrapper function of sys.file.write and
> print that prints the info to the screen, or a trace window and to a
> file accordingly. The string formmating is he same as the python doc.
>
> values in the above code is a integer number.
> The above statement tries to diplay the input string [ from str
> (values ] and convert values to be displayed as hexadecimal.
>
> 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?
>
> Thanks
>
>
>
>





More information about the Python-list mailing list