Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy a Python string?

I do this:

a = 'hello' 

And now I just want an independent copy of a:

import copy  b = str(a) c = a[:] d = a + '' e = copy.copy(a)  map( id, [ a,b,c,d,e ] ) 

Out[3]:

[4365576160, 4365576160, 4365576160, 4365576160, 4365576160] 

Why do they all have the same memory address and how can I get a copy of a?

like image 616
usual me Avatar asked Jul 17 '14 13:07

usual me


People also ask

How do you make a copy of a string?

Copying one string to another - strcpystrcpy can be used to copy one string to another. Remember that C strings are character arrays. You must pass character array, or pointer to character array to this function where string will be copied.

What does Copy () do in Python?

Copy List Python: copy() Method. The Python copy() method creates a copy of an existing list. The copy() method is added to the end of a list object and so it does not accept any parameters. copy() returns a new list.

How do you repeat a string in Python?

In Python, we utilize the asterisk operator to repeat a string. This operator is indicated by a “*” sign. This operator iterates the string n (number) of times.

How to get a new copy of a string in Python?

Since string is immutable, we can get a new copy of the string by adding an empty string to the original string. Also, we can slice the whole string or use str () function to get a copy of a string.

How to copy a string from one Python dictionary to another?

Python deliberately tries to keep just the one copy, as that makes dictionary lookups faster. One way you could work around this is to actually create a new string, then slice that string back to the original content: >>> a = 'hello' >>> b = (a + '.') [:-1] >>> id (a), id (b) (4435312528, 4435312432) But all you are doing now is waste memory.

How to copy one string to another string in C++?

Using the inbuilt function strcpy () from string.h header file to copy one string to the other. strcpy () accepts a pointer to the destination array and source array as a parameter and after copying it returns a pointer to the destination string.

Why does my 'Hello' string only have one copy in Python?

Moreover, your 'hello' string is interned ( certain strings are ). Python deliberately tries to keep just the one copy, as that makes dictionary lookups faster. One way you could work around this is to actually create a new string, then slice that string back to the original content:


2 Answers

You don't need to copy a Python string. They are immutable, and the copy module always returns the original in such cases, as do str(), the whole string slice, and concatenating with an empty string.

Moreover, your 'hello' string is interned (certain strings are). Python deliberately tries to keep just the one copy, as that makes dictionary lookups faster.

One way you could work around this is to actually create a new string, then slice that string back to the original content:

>>> a = 'hello' >>> b = (a + '.')[:-1] >>> id(a), id(b) (4435312528, 4435312432) 

But all you are doing now is waste memory. It is not as if you can mutate these string objects in any way, after all.

If all you wanted to know is how much memory a Python object requires, use sys.getsizeof(); it gives you the memory footprint of any Python object.

For containers this does not include the contents; you'd have to recurse into each container to calculate a total memory size:

>>> import sys >>> a = 'hello' >>> sys.getsizeof(a) 42 >>> b = {'foo': 'bar'} >>> sys.getsizeof(b) 280 >>> sys.getsizeof(b) + sum(sys.getsizeof(k) + sys.getsizeof(v) for k, v in b.items()) 360 

You can then choose to use id() tracking to take an actual memory footprint or to estimate a maximum footprint if objects were not cached and reused.

like image 188
Martijn Pieters Avatar answered Sep 17 '22 13:09

Martijn Pieters


You can copy a string in python via string formatting :

>>> a = 'foo'   >>> b = '%s' % a   >>> id(a), id(b)   (140595444686784, 140595444726400)   
like image 40
Richard Urban Avatar answered Sep 19 '22 13:09

Richard Urban