Nick M.
Nick M.

Reputation: 259

What does 0x%4x mean in python?

I think I understand what % means in python. But I don't understand what the 0x and 4x means. Any help is great even if you just link the documentation for %.

Here is the example code:

wr.write('Base Addr=0x%4x' % 
           (Base_Address))

Upvotes: 0

Views: 1269

Answers (1)

mgilson
mgilson

Reputation: 310117

%4x is a way of formatting a number in hexidecimal

>>> '%4x' % 0xffff
'ffff'
>>> '%4x' % 0xffa1
'ffa1'

The leading 0x will just be literal characters that end up carrying over into the string.

>>> '0x%4x' % 0xffa1
'0xffa1'

The 4 specifies the minimum width (smaller output will be padded on the left with spaces):

>>> '%4x' % 0xff
'  ff'

Upvotes: 5

Related Questions