newbie problem with str.replace

Peter Otten __peter__ at
Wed Aug 4 15:22:22 CEST 2010

BobAalsma wrote:

Although [it] may not be obvious at first unless you're Dutch...

> bestandsnaam_nieuw = bestandsnaam
> bestandsnaam_nieuw.replace(KLANTNAAM_OUT,KLANTNAAM_IN)

str.replace() does not modify a string, it creates a new one.

This doesn't work:
>>> s = "that's all folks"
>>> s.replace("all", "nothing")
"that's nothing folks"
>>> s
"that's all folks"

But this does:

>>> old = "that's all folks"
>>> new = old.replace("all", "nothing")
>>> new
"that's nothing folks"


More information about the Python-list mailing list