0

A snippet of Python code:

def someMethod():
    return datetime.date.today().strftime("%B" + " " + "%d".lstrip('0') + ", " + "%Y")

How come this returns:

June 03, 2015

And not what I expected, which was:

June 3, 2015

Thanks for your help.

salmanwahed
  • 9,450
  • 7
  • 32
  • 55
Aleksey Bilogur
  • 3,686
  • 3
  • 30
  • 57
  • possible duplicate of [Python strftime - date without leading 0?](http://stackoverflow.com/questions/904928/python-strftime-date-without-leading-0) – Andy Jun 03 '15 at 04:45
  • @Andy: I debated it with myself, but in the end decided the fundamental misunderstanding in the question (stripping `"%d"`) warrants a separate answer... – Amadan Jun 03 '15 at 04:46

1 Answers1

1

"%d".lstrip('0') is "%d", as "%d" never had any zeroes in it to begin with. If you are asking if you can operate on different parts formatted by strftime, the answer is no. But you can influence formatting precision.

See this answer for a good alternative, and this answer on the same question for another, possibly non-portable alternative.

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301