Python Forum

Full Version: Format String
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,

I try to print some formatted integers with padded 0.

But there is always a blank character in front of the print.

My code is:
num = 14
print(f"{num: 010d}")
And my output is: 000000014
There are 9 digits and a blank character. Why is there a blank char and not only 10 digits?


THX
The space is there because you have a space in your format string. The space is what you should print as the sign for a positive number.
num = 14
print(f"{num:010d}")  # No prefix for positive numbers
print(f"{-num:010d}")
print(f"{num:+010d}") # + prefix for positive numbers
print(f"{-num:+010d}")
print(f"{num: 010d}")  # space prefix for positive numbers
print(f"{-num: 010d}")
Output:
0000000014 -000000014 +000000014 -000000014 000000014 -000000014
Oh, ok. Thanks for your answer.
I didn't know, that the space between ':' and '0' counts.