Python strftime: date or month without a leading zero
Codemia Knowledge Hub ·
To drop the leading zero in Python's strftime, use %-d and %-m on Unix-like systems (Linux, macOS) or %#d and %#m on Windows. Neither is portable, so for cross-platform code the safest answer is to format the parts yourself, for example f"{dt.month}/{dt.day}/{dt.year}".
The default: zero-padded %d and %m
The standard directives pad day and month with zeros:
Output:
That is often what you want for logs, filenames and stable machine-readable formats.
Linux and macOS: %-d and %-m
On Linux and macOS, the minus flag usually removes zero padding:
Typical output:
This is convenient, but it is not guaranteed to behave the same way on Windows.
Windows: %#d and %#m
The Windows 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.
Portable option: format the parts yourself
If portability matters, build the string from the integer date components:
This avoids platform-specific strftime flags entirely.
To get March 7, 2026 without relying on day formatting directives, mix strftime for the text part with the integer day:
This is often the most practical solution when only one or two components need non-padded output.
Why the flags differ by platform
In many environments Python's strftime depends on the platform C library. Python exposes the formatting interface, but platforms do not all support the same modifier flags. That is why:
%dis broadly portable%-dis common but not universal%#dis a Windows-specific convention
The portability problem comes from the underlying system implementation, not from Python syntax.
A reusable helper
Rather than scattering platform-specific directives through the codebase, write a small helper:
The behavior is obvious and there is no need to branch on the operating system for common display formats. If month names or localized output matter, you can still use strftime for the text parts and assemble the numbers manually.
When to keep leading zeros
Do not remove padding 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 line up as neatly in fixed-format output. The real question is whether the string should be human-friendly or system-friendly. Avoid platform-specific flags in cross-platform libraries unless you have tests on each platform.
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
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.