Fastest way to insert these dashes in a python string?

So, I know that Python strings are immutable, but I have a string:

c['date'] = "20110104" 

What I would like to convert to

 c['date'] = "2011-01-04" 

My code is:

 c['date'] = c['date'][0:4] + "-" + c['date'][4:6] + "-" + c['date'][6:] 

Seems a little confusing, no? Would it be better to save it as a separate variable and then do the same? Or basically there will be no difference?

+7
source share
7 answers

You can use .join() to clean it up a bit:

 d = c['date'] '-'.join([d[:4], d[4:6], d[6:]]) 
+12
source

You are better off using string formatting than string concatenation

 c['date'] = '{}-{}-{}'.format(c['date'][0:4], c['date'][4:6], c['date'][6:]) 

String concatenation is usually slower because, as you said above, strings are immutable.

+6
source
 s = '20110104' def option_1(): return '-'.join([s[:4], s[4:6], s[6:]]) def option_1a(): return '-'.join((s[:4], s[4:6], s[6:])) def option_2(): return '{}-{}-{}'.format(s[:4], s[4:6], s[6:]) def option_3(): return '%s-%s-%s' % (s[:4], s[4:6], s[6:]) def option_original(): return s[:4] + "-" + s[4:6] + "-" + s[6:] 

Running %timeit on each gives these results

  • option_1: 35.9 ns per loop
  • option_1a: 35.8 ns per cycle
  • option_2: 36 ns per cycle
  • option_3: 35.8 ns per cycle
  • option_original: 36 ns per loop

So ... choose the most readable one since the performance improvements are minor.

+3
source

Dates are first class objects in Python, with a rich interface for managing them. Datetime library.

 > import datetime > datetime.datetime.strptime('20110503','%Y%m%d').date().isoformat() '2011-05-03' 

Do not reinvent the wheel!

+3
source

I would probably do it this way, and not that it worked out a lot:

 d = c['date'] c['date'] = '%s-%s-%s' % (d[:4], d[4:6], d[6:]) 

A big improvement (imho) avoids string concatenation.

+1
source

I'm not sure if you want to convert it to the corresponding datetime object, or rather just with hard code in the format, you can do the following:

 from datetime import datetime result = datetime.strptime(c['date'], '%Y%m%d') print result.date().isoformat() 

Entrance: '20110104'

Exit: '2011-01-04'

+1
source

I usually don’t say "use regex", but this is a good use case:

 import re c['date']=re.sub(r'.*(\w{4})(\w{2})(\w{2}).*',r"\1-\2-\3",c['date']) 
0
source

All Articles