[Tutor] Operating in Place

Steven D'Aprano steve at pearwood.info
Tue Sep 28 23:46:15 CEST 2010


On Wed, 29 Sep 2010 06:03:21 am Corey Richardson wrote:
>   Hello tutors.
>
> I hate doing this:
>              string = string.lower()
>
> Is there a way to do it without the "string =" part? Thanks.

No, strings are immutable. Once they're created, they cannot be changed.

This is no different from:

x = 42
x = x/2  # or x /= 2 if you prefer

instead of:

x = 42
x/2


As an alternative you could look at the UserString module, and the 
MutableString class it includes, but MutableString has some serious 
limitations and is not really meant for serious work. But it might do 
the job you want.



-- 
Steven D'Aprano


More information about the Tutor mailing list