So I know Python strings are immutable, but I have a string:
c['date'] = "20110104"
Which I would like to convert to
c['date'] = "2011-01-04"
My code:
c['date'] = c['date'][0:4] + "-" + c['date'][4:6] + "-" + c['date'][6:]
Seems a bit convoluted, no? Would it be best to save it as a separate variable and then do the same? Or would there basically be no difference?
You could use .join()
to clean it up a little bit:
d = c['date']
'-'.join([d[:4], d[4:6], d[6:]])