0

I want to print some integer values in Hex. For this purpose I normally use format() function. Suppose I want to print X in place of 0 in the output will format() help me to do that?

>>> format(16,"04X")
'0010'

what i need :

'XX10'

1 Answer 1

2

If you read the Format Specification Mini-Language, you'll see that the 0 you're using is a special case:

Preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='.

That should give you the clue you need to do the same thing with a different fill character. But just in case, let's look at what it says for those align and fill fields:

If a valid align value is specified, it can be preceded by a fill character that can be any character and defaults to a space if omitted.

The syntax for the format string is:

format_spec ::=  [[fill]align][sign][#][0][width][,][.precision][type]

So, that tells you where to put the fill and align values:

format(16, "X=4X")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.