Iteration over strings

Robert Dailey rcdailey at
Tue Jul 31 22:07:09 CEST 2007


Thanks a lot for your answers guys. I had already known that strings are
immutable, but having been a C++ programmer for years I'm still trying to
get over the fact that it's not std::string :) The python documentation
isn't that easy to navigate in my opinion, so I wasn't able to find the
'replace()' function you guys are talking about. It's actually the perfect
solution for the problem.

I appreciate your time.

On 7/31/07, Hexamorph <hexamorph at> wrote:
> Jay Loden schrieb:
> > Robert Dailey wrote:
> >> str = "C:/somepath/folder/file.txt"
> >>
> >> for char in str:
> >>     if char == "\\":
> >>         char = "/"
> > strings in Python are immutable - in other words, they cannot be updated
> in place as you're doing above. However, that doesn't mean you can't achieve
> what you're after. In Python, the way to approach this problem since you
> cannot modify the string in place, is to create a new string object with the
> desired content:
> Also note, that you are just extracting one char from the string
> into another variable (which you then change) and you are *not*
> getting any "reference" to the char in the string.
> As has been alreagy told, use the str.replace() function
> --
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list