Python Strings

nsiam at my-deja.com nsiam at my-deja.com
Sun Aug 20 04:51:50 EDT 2000


fromstr = "aslfdj"
tostr = fromstr[:]

-or-

import copy
fromstr = "aslfdj"
tostr = copy.copy(fromstr)

both copy the string, if that's what you want...

the way i've seen the term immutable/mutable used in some python
literature is to help explain what parameters are passed by reference.
sift through the handouts/slides available in python.org...one of them
does a good job explaining immutable/mutables.

In article <399F863A.48853BCD at ntlworld.com>,
  Richard Chamberlain <richard_chamberlain at ntlworld.com> wrote:
> Hi Chris,
>
> fromstr="aslfdj"
> tostr=fromstr
>
> Is that what you want?
>
> If you change fromstr to another string tostr will remain pointing at
> the first object "aslfdj".
>
> Or you could do something like
>
> tostr=""
> tostr.append(fromstr)
>
> strings are immutable so you don't have to worry about putting const
in
> front of them ;-)
>
> immutable types in python are useful for dictionaries essentially. For
a
> hashtable to be efficient it requires an unchanging key, so strings
fit
> this bill.
>
> Richard
>
> CHRIS wrote:
> >
> > How can you make a character-by-character copy of one string to a
target
> > string, with out using a temporary list type variable? IE (with out
> > doing somethin like this):
> >
> > fromstr = 'aslfdj';
> > target = [];
> > for i in range(len(fromstr)):
> >   #procssing done here
> >   target.append(fromstr[i]);
> >
> > Also, what's the point of strings being immutable ?? If you want a
> > particular variable to be immutable, why not have some sort of
constant
> > option? Like in C:
> >
> > static const char str[] = "Constant immutable string";
>


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list