[Tutor] Mutable String Question
Roeland Rengelink
Roeland.Rengelink@chello.nl
Wed Aug 6 18:11:21 EDT 2003
Hi Marc,
Probably the easiest string 'buffer' is the list, combined with the
string method 'join'
Your example:
>>> word_list = ['My', 'name', 'is']
>>> a_string = ' '.join(word_list)
>>> a_string
'My name is'
The method join is called on a separator (in this case a space ' '), and
takes a list of string as an argument. The new string contains all the
strings from the list, sepated by the separator.
Hope this helps,
Roeland
On Mon, 2003-08-04 at 02:56, Marc Barry wrote:
> All:
>
> Is there a mutable string type such as a string buffer in Python? I know
> that string's in Python are immutable and therefore you cannot append to
> them. It seems terribly inefficient to do something like the following:
>
> #------
>
> a_string = ""
> word_list = ["My", "name", "is"]
>
> for word in word_list:
> a_string = a_string + " " + word
>
> print a_string
>
> #------
>
> I know that the above is a trivial example, but with the amount of text I am
> processing this could have a significant impact on performance. Does anyone
> know of a better way to handle this?
>
> Regards,
>
> Marc
>
> _________________________________________________________________
> Protect your PC - get McAfee.com VirusScan Online
> http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list