[Tutor] Replace a character by index

Wayne srilyk at gmail.com
Thu Jul 16 14:45:14 CEST 2009


On Thu, Jul 16, 2009 at 5:50 AM, Kent Johnson <kent37 at tds.net> wrote:

> On Thu, Jul 16, 2009 at 4:29 AM, Wayne<srilyk at gmail.com> wrote:> I have
> two different ways I can put _ in the word:
> > word = 'cat'
> >
> > ''.join(list(word)[1] = '_')
>
> Not in any Python I ever used...
> In [1]: word = 'cat'
>
> In [2]:
>
> In [3]: ''.join(list(word)[1] = '_')
> ------------------------------------------------------------
>   File "<ipython console>", line 1
> SyntaxError: keyword can't be an expression (<ipython console>, line 1)

<snip>
> Um, the second method is faster and has the advantage of working :-)


whoops! What I meant to say was something a bit more like:

word = list(word)
word[1] = '_'
word = ''.join(word)

But that's a bit harder to put in a timer so I just used
''.join(list('foo'))

Evidently my early morning translation for brevity also translated out
successful syntax, doh!
-Wayne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090716/2594ccf9/attachment.htm>


More information about the Tutor mailing list