How can you copy (clone) a string?
Max Møller Rasmussen
maxm at normik.dk
Tue Oct 3 16:14:21 CEST 2000
Just use slice:
a = 'a'
b = a[:]
From: Peter.Rupp at ual.com [mailto:Peter.Rupp at ual.com]
Sent: Tuesday, October 03, 2000 4:09 PM
To: python-list at python.org; Peter.Rupp at ual.com
Subject: How can you copy (clone) a string?
I have a bonafide need to create copies of strings in a python program
(as opposed to a reference to the original). I tried using the 'copy'
module using both it's 'copy' and 'deepcopy' methods to no avail. I'm
using the "id" function to determine whether I have a new string or a
reference to an existing string.
Let me illustrate......
from copy import *
Huh? This is supposed to give me a new string refererence...as noted
in the book 'Python Essential Reference' by David M. Beazley (pp. 95 in
the last paragraph under the heading "NOTES", where he describes the
feature as "This module can be used with simple types such as integers
and strings, but there's little need to do so."
Well, I really do have a need here ;-)
Any help or suggestions would be appreciated...please respond via email
as I cannot get news here. Thanks in advance.
P. A. Rupp - United Airlines Corp.
peter.rupp at ual.com
More information about the Python-list