Aleksey Bilogur
Aleksey Bilogur

Reputation: 3846

Can I operate on formatted strings?

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.

Upvotes: 0

Views: 33

Answers (1)

Amadan
Amadan

Reputation: 198304

"%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.

Upvotes: 1

Related Questions