How can I copy a Python string?

后端 未结 5 982
深忆病人
深忆病人 2020-12-12 23:38

I do this:

a = \'hello\'

And now I just want an independent copy of a:

import copy

b = str(a)
c = a[:]
d = a          


        
5条回答
  •  醉酒成梦
    2020-12-12 23:47

    Copying a string can be done two ways either copy the location a = "a" b = a or you can clone which means b wont get affected when a is changed which is done by a = 'a' b = a[:]

提交回复
热议问题