Need a specific sort of string modification. Can someone help?

Chris Angelico rosuav at
Sat Jan 5 10:27:46 CET 2013

On Sat, Jan 5, 2013 at 7:35 PM, Sia <hossein.asgharian at> wrote:
> I have strings such as:
> tA.-2AG.-2AG,-2ag
> or
> .+3ACG.+5CAACG.+3ACG.+3ACG
> The plus and minus signs are always followed by a number (say, i). I want python to find each single plus or minus, remove the sign, the number after it and remove i characters after that. So the two strings above become:
> tA..,
> and
> ...
> How can I do that?

Interesting. Are you guaranteed that there are no other plus or minus
signs? Is the number after the sign just one digit? Assuming the
answers to both are "yes", here's a one-liner:


result = "".join([x[int(x[0])+1:] for x in ("0"+s).replace("-","+").split("+")])

Split on either - or +, then trim off that many characters from each
(that's the list comp), then join them back into a string.


More information about the Python-list mailing list