Fastest way to insert these dashes in python string?

前端 未结 8 1487
失恋的感觉
失恋的感觉 2021-02-13 16:34

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

c[\'date\'] = \"20110104\"

Which I would like to convert to

c[\'da         


        
8条回答
  •  逝去的感伤
    2021-02-13 17:07

    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 generally slower because as you said above strings are immutable.

提交回复
热议问题