Python Forum
Format String - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Format String (/thread-38415.html)



Format String - NewPi - Oct-09-2022

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


RE: Format String - deanhystad - Oct-10-2022

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



RE: Format String - NewPi - Oct-10-2022

Oh, ok. Thanks for your answer.
I didn't know, that the space between ':' and '0' counts.