replacing substrings within strings

Paul McGuire ptmcg at austin.rr.com
Wed Feb 14 09:31:14 EST 2007


On Feb 14, 6:08 am, "amadain" <mfmdev... at gmail.com> wrote:
> Hi
> 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)
>
> The above code works fine but I was wondering if anybody had another
> way of doing this?
>
> A

"4th and 5th characters" -> darr[4:6]

You must be referring to the leading '0' as the 0th character, then.

-- Paul




More information about the Python-list mailing list