How can I copy a Python string?

后端 未结 5 989
深忆病人
深忆病人 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:56

    You can copy a string in python via string formatting :

    >>> a = 'foo'  
    >>> b = '%s' % a  
    >>> id(a), id(b)  
    (140595444686784, 140595444726400)  
    

提交回复
热议问题