translate maketrans

RNA Transcription
RNA Transcription in Python
LOOKUP = str.maketrans("GCTA", "CGAU")


def to_rna(dna_strand):
    return dna_strand.translate(LOOKUP)

This approach starts by defining a dictionary (also called a translation table in this context) by calling the maketrans() method.

Python doesn't enforce having real constant values, but the LOOKUP translation table is defined with all uppercase letters, which is the naming convention for a Python constant. It indicates that the value is not intended to be changed.

The translation table that is created uses the ASCII values (also called the ordinal values) for each letter in the two strings. The ASCII value for "G" in the first string is the key for the ASCII value of "C" in the second string, and so on.

In the to_rna() function, the translate() method is called on the input, and is passed the translation table. The output of translate() is a string where all of the input DNA characters have been replaced by their RNA complement in the translation table.

Note

As of this writing, no invalid DNA characters are in the argument to to_rna(), so there is no error handling required for invalid input.

4th Sep 2024 · Found it useful?