String Replacement

Gary Herron gherron at
Sat Aug 2 04:33:22 CEST 2003

On Friday 01 August 2003 03:00 pm, Fazer wrote:
> Hello,
> As some of you might know, I am new to Python.  I was finally
> successful in using MySQLdb for Python to query the datbase and get a
> few results from the query.
> The results will be displayed as HTML and I am sort of confuse din
> using the string modules and its functions/modules to replace the \n
> with <br>.  I import the string module and I am not soo sure on how to
> do the replacing.
> Here's how my code sort of likes:
> import string
> string.replace(DataFromDatabase, "\n", "<br>")
> Unfortunately, it doesn't work.

The replacement you are trying to acccomplish probably does happen,
but you ignore the returned string.  Try:

  DataTransformed = string.replace(DataFromDatabase, "\n", "<br>")

Another way to say this is that strings are immutable -- you can't
change the contents of a string.  Any functions such as these return
*new* strings with the desired contents.

This is a common beginners mistake, but you'll be past these things

Gary Herron

More information about the Python-list mailing list