replacing substrings within strings

amadain mfmdevine at gmail.com
Wed Feb 14 07:27:39 EST 2007


On Feb 14, 12:16 pm, "Diez B. Roggisch" <d... at nospam.web.de> wrote:
> amadain 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?
>
> You can do it like this:
>
> darr=list("010203040506")
> darr[:2], darr[4:5] = darr[4:6], darr[:2]
> result="".join(darr)
> print result
>
> Diez



Thats the same code. I was wondering if the string manipulation can be
done without an excursion into the list world.

A




More information about the Python-list mailing list