Q:

Truncating integers in format() in Python 2.7

Using the "old" formatting syntax I can truncate long integers in a string, like this:
'%-5.5s' % 4257647474747

which produces 42576
If I try to do the same with format():
'{:<5.5}'.format(4257647474747)

I get the error ValueError: Precision not allowed in integer format specifier
I need to be able to truncate the incoming number, because it must fit in a fixed size string. Is there any way to truncate the integer using format?

A:

The s in '%-5.5s' converts arguments to strings with str and then applies %s's interpretations for the -5.5. You're not doing anything similar in your format call, so you're getting the int type's interpretation of <5.5.
Convert your integer to a string before formatting it:
'{:<5.5}'.format(str(4257647474747))

or use !s in the format string to do the same:
'{!s:<5.5}'.format(4257647474747)

