ValueError: zero length field name in format in Python2.6.6

Question:

I use this python shell to generate a string:

>>>':'.join("{:x}n".format(random.randint(0, 2**16 - 1)) for i in range(4))

When I run this shell in Python2.7.5, everything goes okay. But it occurs ValueError: zero length field name in format when Python version is 2.6.6. What should I run this shell fine when Python version is 2.6.6?

Asked By: changzhi

||

Answers:

In Python versions 2.6 or earlier, you need to explicitly number the format fields:

':'.join("{0:x}n".format(random.randint(0, 2**16 - 1)) for i in range(4))
#          ^

You can read about this in the docs:

Changed in version 2.7: The positional argument specifiers can be omitted, so '{} {}' is equivalent to '{0} {1}'.

Answered By: user2555451
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.