[Tutor] substitute function

Kent Johnson kent37 at tds.net
Sun Jul 31 21:46:01 CEST 2005

Srinivas Iyyer wrote:
> Hello group:
> Is there a 'substitute' function in python.
> For example:
> I want to substitute A with T and G with C and vice
> versa
> A -> T
> G -> C
> T -> A
> c -> G

You can do this with the translate() method of a string. It is a two-step process. First you have to make a translation table that defines the translation. Do this with string.maketrans():
 >>> import string
 >>> xlate = string.maketrans('AGTC', 'TCAG')

Next use the translation table to translate the string of interest:
 >>> 'AAGTTC'.translate(xlate)


More information about the Tutor mailing list