Python strftime - date without leading 0?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Python's strftime supports zero-padded directives such as %d and %m, but removing the leading zero is not completely portable across operating systems. On many Unix-like systems, %-d and %-m work. On Windows, %#d and %#m are the usual alternatives. If you need portable behavior, manual formatting is often the safest answer.
The Normal Zero-Padded Format
Standard directives pad day and month with zeros:
Output:
That is often exactly what you want for logs, filenames, and stable machine-readable formats.
Unix-Like Systems: %-d and %-m
On Linux and macOS, the minus flag commonly removes zero padding:
Typical output:
This is convenient, but it is not guaranteed to work the same way on Windows.
Windows: %#d and %#m
On Windows, the platform strftime implementation commonly uses # instead of - for non-padded output.
That often produces the same visible result, but it ties the format string to the platform.
The Portable Option: Format the Parts Yourself
If portability matters, build the string from the integer date components directly.
This avoids platform-specific strftime flags entirely.
For example, if you want March 7, 2026 without relying on day formatting directives:
This is often the most practical solution when only one or two components need non-padded output.
Why Platform Differences Exist
Python's strftime behavior ultimately depends on the underlying platform C library in many environments. Python exposes the formatting interface, but not every platform supports exactly the same modifier flags.
That is why:
- '
%dis broadly portable' - '
%-dis common but not universal' - '
%#dis a Windows-specific convention'
The portability problem is not about Python syntax alone. It comes from the underlying system implementation.
A Safe Helper Function
If you need reusable portable formatting, write a small helper instead of scattering platform-specific directives through the codebase.
This keeps the behavior obvious and removes the need to branch on operating system details for common display formats.
If month names or localized output matter, you may still use strftime for the text parts while assembling numeric pieces manually.
When You Should Keep Leading Zeros
Do not remove padding automatically just because a UI example looks nicer without it. Leading zeros are useful for:
- lexicographically sortable strings
- stable log formats
- file naming
- machine-readable protocols
For example, 2026-03-07 sorts correctly as a string, while 2026-3-7 does not align as neatly in fixed-format outputs.
So the real question is not "how do I remove the zero?" It is "should this string be human-friendly or system-friendly?"
Common Pitfalls
- Assuming
%-dworks everywhere. It usually does not on Windows. - Assuming
%#dis portable. It is mainly a Windows solution. - Using platform-specific
strftimeflags in cross-platform libraries without tests. - Removing zero padding in values that are better kept machine-stable, such as log or filename formats.
- Overcomplicating the solution when manual formatting with
dt.dayanddt.monthwould be clearer.
Summary
- '
%dand%mare zero-padded by default instrftime.' - '
%-dand%-mcommonly remove padding on Unix-like systems.' - '
%#dand%#mare the usual Windows equivalents.' - For portable non-padded formatting, manual string construction is often the cleanest option.
- Keep leading zeros when the date string is meant for sorting, protocols, or stable machine-oriented output.
Related reading
- Python String clustering with scikit-learn''s dbscan, using Levenshtein distance as metric
- Python string 'in' operator implementation algorithm and time complexity
- Python string.replace regular expression
- Python strings and integer concatenation
- python subprocess callback when updated output
- Python subprocess is not scalable by default, any simple solution you can recommend to make it scalable?
- Python subprocess/Popen with a modified environment
- python swapped tuple to dict
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.