HOWTO · Python

How to Get the Reverse Complement of a DNA Strand Using Python

This article discusses how we can create a reverse complement of a DNA strand using Python. It uses the in-built string methods and the Biopython module.

On this page

For the DNA strand ACGTAATTGGCC, the reverse complement is GGCCAATTACGT. Python produces it by complementing each base and reading the bases from the end to the beginning.

Reverse Complement of a DNA Strand

DNA uses the base pairs A↔T and C↔G. The complement of ACGTAATTGGCC is TGCATTAACCGG; reversing that complement gives GGCCAATTACGT. Reversal changes order, while complementation changes each base.

The examples below map uppercase A, T, C, and G. They preserve other symbols as written, including lowercase letters; they do not normalize case or validate a complete DNA alphabet.

Get the Reverse Complement of a DNA Strand Using the for Loop in Python

For an uppercase DNA sequence, this complete example builds the result with a for loop:

input_strand = "ACGTAATTGGCC"
complements = {"A": "T", "T": "A", "C": "G", "G": "C"}
result = ""
for base in reversed(input_strand):
    result += complements.get(base, base)
print(result)
GGCCAATTACGT

The loop visits bases in reverse order. The dictionary fallback keeps a symbol unchanged when it is not one of the four uppercase bases.

Get the Reverse Complement of a DNA Strand Using the join() Method in Python

Collecting characters in a list and joining them makes the assembly step clear:

input_strand = "ACGTAATTGGCC"
complements = {"A": "T", "T": "A", "C": "G", "G": "C"}
complement_chars = [complements.get(base, base) for base in reversed(input_strand)]
result = "".join(complement_chars)
print(result)
GGCCAATTACGT

join() combines the list elements into one string. This standard-library approach avoids repeatedly extending the result string.

Get the Reverse Complement of a DNA Strand Using the get() Method in Python

Here, base is the dictionary key. get(base, base) returns the mapped complement when the key exists and returns the original character as the fallback otherwise:

input_strand = "ACGTAATTGGCC"
complements = {"A": "T", "T": "A", "C": "G", "G": "C"}
result_chars = []
for base in reversed(input_strand):
    result_chars.append(complements.get(base, base))
print("".join(result_chars))
GGCCAATTACGT

The list-and-join form keeps the loop readable while assembling a string from characters.

Get the Reverse Complement of a DNA Strand Using List Comprehension in Python

The reverse slice [::-1] visits the input from right to left. Supplying the original character as the fallback ensures every list item is a string:

input_strand = "ACGTAATTGGCC"
complements = {"A": "T", "T": "A", "C": "G", "G": "C"}
result = "".join(complements.get(base, base) for base in input_strand[::-1])
print(result)
GGCCAATTACGT

Without that fallback, an unmapped base would produce None, which join() cannot combine with strings.

Get the Reverse Complement of a DNA Strand Using the translate() Method in Python

The standard-library str.maketrans() creates a character mapping table; str.translate() applies it. Reverse the strand separately, then translate each base:

input_strand = "ACGTAATTGGCC"
table = str.maketrans("ATCG", "TAGC")
result = input_strand[::-1].translate(table)
print(result)
GGCCAATTACGT

Characters absent from the table pass through unchanged. This approach needs no external library.

Get the Reverse Complement of a DNA Strand Using the Biopython Module in Python

Biopython is an external dependency that provides sequence utilities, including reverse_complement(). Its distribution name is biopython, while the Python import path begins with Bio. Install it in the environment used to run your script with python -m pip install biopython.

The following is a syntax illustration, not a runtime-verified example: installation and import have not been confirmed in the selected runtime environment.

from Bio.Seq import Seq and Seq("ACGTAATTGGCC").reverse_complement()

This option depends on Biopython being installed; the standard-library examples above do not. Use the library when its sequence utilities fit your project, or choose a standard-library approach when adding a dependency is unsuitable.

Conclusion

Use a loop, dictionary, list comprehension, or translate() for a standard-library solution. Choose Biopython when its broader sequence utilities suit the task. An empty string produces an empty result. The examples preserve other symbols and lowercase input characters; normalize or validate input separately if your data requires that policy.