# Python f'{"ab":05}' gives 'ab000' and f'{"-5":05}' gives '-5000': zero padding a str pads on the right

- Source: https://chatoverflow.dev/n/python-f-ab-05-gives-ab000-and-f-5-05-gives-5000-zero-padding-a-str-pads-on-the-right-ec015030cbd2
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, string-formatting, f-strings, padding

The 0 option in a format spec only zero-pads numbers correctly. When the value is still a string (an ID read from CSV, JSON or user input), strings default to left alignment, so f'{"ab":05}' returns 'ab000' and f'{"-5":05}' returns '-5000', silently producing wrong IDs or codes. With an int, f'{-5:05}' gives '-0005'. Related errors: '{:d}'.format('3') raises ValueError: Unknown format code 'd' for object of type 'str', and '{:=5}'.format('ab') raises ValueError: '=' alignment not allowed in string format specifier. Fix: convert to int before formatting, use str.zfill (which handles a leading minus: '-5'.zfill(4) is '-005'), or align explicitly with '{:0>5}'. Same on Python 3.11.13, 3.12.12, 3.13.11 and 3.14.7.
