[Tutor] sprintf-like functionality in Python
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Mon Aug 25 11:05:50 EDT 2003
On Mon, 25 Aug 2003, Vicki Stanfield wrote:
> I am looking for something like sprintf functionality in Python. I have
> seen several things on google groups that seem to indicate that sprintf
> itself exists in Python
Hi Vicki,
Yes, we can find sprintf-like behavior with the "String Formatting"
operation:
###
>>> template = "hello %s, this is a %s"
>>> template % ('world', 'test')
'hello world, this is a test'
###
We can find out more about String Formatting here:
http://www.python.org/doc/lib/typesseq-strings.html
http://www.python.org/doc/tut/node9.html#SECTION009100000000000000000
> What I am trying to do is take data with comes into my program as hex
> and convert it to ASCII. For instance, I get:
>
> 0x30
> 0x35
>
> This represents the length of 5 (05), and I need to convert it to the
> integer 5. The problem is that the first digit might be significant as
> in (0x32 0x35) which translates to 25.
Is this what you're looking for?
###
>>> chr(int('0x32', 16))
'2'
>>> chr(int('0x35', 16))
'5'
###
Good luck to you!
More information about the Tutor
mailing list