Why does “%-d”, or “%-e” remove the leading space or zero?

Python datetime.strftime() delegates to C strftime() function that is platform-dependent:

The full set of format codes supported varies across platforms,
because Python calls the platform C library’s strftime() function, and
platform variations are common. To see the full set of format codes
supported on your platform, consult the strftime(3) documentation.

Glibc notes for strftime(3):

(dash) Do not pad a numeric result string.

The result on my Ubuntu machine:

>>> from datetime import datetime
>>> datetime.now().strftime('%d')
'07'
>>> datetime.now().strftime('%-d')
'7'

Leave a Comment