[Tutor] methods split and join in sequence

W W srilyk at gmail.com
Sat Jan 31 16:52:02 CET 2009


On Sat, Jan 31, 2009 at 9:09 AM, David <ldl08 at gmx.net> wrote:

> Dear list,
>
> many thanks for all your help - much appreciated.
> Today I have continued reading Harrington, and produced the following code,
> which works flawlessly.
> I wonder, though, how I could join all the supplied words with underscores
> _without_ making use of split first? This I didn't manage. <snip>


I'm really not sure if you could. Strings don't support item reassignment,
so you can't do it this way:

 In [1]: x = 'foo bar'

In [2]: x[3] = '_'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

C:\Documents and Settings\Wayne\<ipython console> in <module>()

TypeError: 'str' object does not support item assignment

But there are other string methods available, such as the replace method.
http://docs.python.org/library/string.html
for the string documentation.

HTH,
Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090131/ec38b829/attachment.htm>


More information about the Tutor mailing list