replacing substrings within strings

Sion Arrowsmith siona at chiark.greenend.org.uk
Wed Feb 14 07:45:35 EST 2007


amadain <mfmdevine at gmail.com> wrote:
>I was wondering if there was a nicer way to swap the first 2
>characters in a string with the 4th and 5th characters other than:
>
>darr=list("010203040506")
>aarr=darr[:2]
>barr=darr[4:6]
>darr[:2]=barr
>darr[4:6]=aarr
>result="".join(darr)

darr=list("010203040506")
darr[:2], darr[4:6] = darr[4:6], darr[:2]
result = "".join(darr)

-- 
\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