string building

Sion Arrowsmith siona at chiark.greenend.org.uk
Mon Apr 3 10:58:03 EDT 2006


John Salerno  <johnjsal at NOSPAMgmail.com> wrote:
>Out of curiosity, is there any kind of equivalent in Python to the 
>StringBuilder class in C#? Here's a quick description from the .NET 
>documentation:
>
>"This class represents a string-like object whose value is a mutable 
>sequence of characters. The value is said to be mutable because it can 
>be modified once it has been created by appending, removing, replacing, 
>or inserting characters."

You can get a long way with list of characters wrapped in list() and
"".join(). Seems to me like the main gotcha is to be careful with things
like:
>>> mutable_string = list("initial\nvalue")
>>> mutable_string[7] = "\r\n"
and make sure you do:
>>> mutable_string[7:8] = "\r\n"

(Although note that you have to spell find() and other string
methods "".join(mutable_string).find("value").)

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list