string transformation - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: string transformation (/thread-9663.html) |
string transformation - kerzol81 - Apr-22-2018 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 RE: string transformation - Gribouillis - Apr-22-2018 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? RE: string transformation - kerzol81 - Apr-22-2018 Thanks. It's gonna be %Y-%m-%d format. |