Python
strftime
date formatting
leading zeros
datetime

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.

Browse interview questions

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:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4print(dt.strftime("%Y-%m-%d"))
5print(dt.strftime("%m/%d/%Y"))

Output:

text
2026-03-07
03/07/2026

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:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4print(dt.strftime("%-m/%-d/%Y"))

Typical output:

text
3/7/2026

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.

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4print(dt.strftime("%#m/%#d/%Y"))

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.

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4formatted = f"{dt.month}/{dt.day}/{dt.year}"
5print(formatted)

This avoids platform-specific strftime flags entirely.

For example, if you want March 7, 2026 without relying on day formatting directives:

python
1from datetime import datetime
2
3dt = datetime(2026, 3, 7)
4formatted = f"{dt.strftime('%B')} {dt.day}, {dt.year}"
5print(formatted)

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:

  • '%d is broadly portable'
  • '%-d is common but not universal'
  • '%#d is 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.

python
1from datetime import date
2
3def format_date_no_leading_zero(value):
4    return f"{value.month}/{value.day}/{value.year}"
5
6
7print(format_date_no_leading_zero(date(2026, 3, 7)))

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 %-d works everywhere. It usually does not on Windows.
  • Assuming %#d is portable. It is mainly a Windows solution.
  • Using platform-specific strftime flags 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.day and dt.month would be clearer.

Summary

  • '%d and %m are zero-padded by default in strftime.'
  • '%-d and %-m commonly remove padding on Unix-like systems.'
  • '%#d and %#m are 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.