Regular expression

Fernando Pérez fperez528 at yahoo.com
Sun Nov 25 19:02:19 EST 2001


> Sang DeSibtis wrote:
>> 
>> 
>>  Questions: how do I make a global replacements in the 'line' that
>> read from the file object (buffer in memory? ).

The key thing to understand is that in Python, strings are immutable, so you 
can never do *anything* in place to a string. All string operations return a 
new string, which if you want to work 'in place' you can just reassign to 
your original string:

  string = do_whatever(string)

Even string methods don't work in-place:

In [1]: s='abc'
In [2]: s.replace('a','A')
Out[2]= 'Abc'
In [3]: s
Out[3]= 'abc'

Cheers,

f



More information about the Python-list mailing list