Python Forum

Full Version: string transformation
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

Could you tell me the most pythonic way to make this transformation:

day='181212'
newday='2018-12-12'
I did this, but I don't like it:
newday = '20' + day[0] + day[1] + '-' + day[2] + day[3] + '-' + day[4] + day[5]
Thanks
I would use
import datetime
day = '181212'
newday = datetime.datetime.strptime(day, '%y%d%m').strftime('%Y-%d-%m')
Note that 12 12 is ambiguous. Is it day/month or month/day?
Thanks. It's gonna be %Y-%m-%d format.