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

后端 未结 1 1728
执念已碎
执念已碎 2020-12-06 00:08

On SO question 904928 (Python strftime - date without leading 0?) Ryan answered:

Actually I had the same problem and I realised that, if you add a h

1条回答
  •  执念已碎
    2020-12-06 00:25

    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'
    

    0 讨论(0)
提交回复
热议问题