DNA and RNA: the code of life, as strings
DNA is life's instruction manual, written in just four letters. To a programmer, a strand of DNA is simply a string — and that means you can read and transform it with code.
The big idea
DNA stores genetic instructions as a sequence of four bases (A, T, C, G); RNA is a working copy that uses U in place of T.
See it in code
In the biotech lab, a sequence is just text — DNA is a string built from the letters A, T, C, and G. That means every string tool you know already works on it, starting with indexing and len:
dna = "ATGGCTTAA"
print("Sequence:", dna)
print("First base:", dna[0])
print("Length:", len(dna), "bases")Sequence: ATGGCTTAA First base: A Length: 9 bases
dna[0] reaches the first base and len(dna) counts them all — the same moves you'd make on any string, now reading genetic data.
Inspecting goes further than one base. .count() tallies how often a letter appears, and slicing pulls out a stretch — here the first three bases, the start of the strand:
dna = "ATGGCTTAA"
print("A count:", dna.count("A"))
print("T count:", dna.count("T"))
print("First three bases:", dna[0:3])A count: 3 T count: 3 First three bases: ATG
dna.count("T") finds all three Ts — and every one of them is what transcription is about to change.
Inspecting a strand is useful; transforming it is the payoff. To make its RNA copy, the cell swaps every T for a U — and in code, that's a single .replace("T", "U"):
dna = "ATGGCTTAA"
rna = dna.replace("T", "U")
print("DNA:", dna)
print("RNA:", rna)
print("Length:", len(dna), "bases")DNA: ATGGCTTAA RNA: AUGGCUUAA Length: 9 bases
One string operation captured what a cell does during transcription: those three Ts become Us. Because a sequence is just text, every string tool you know — len, indexing, slicing, count — now works on genetic data.
Treating biology as data is the whole field of bioinformatics. Genomes are gigabyte-long strings; code searches them for genes, compares them across species, and spots mutations. The same string skills that clean user input let you analyze the code of life.
Try it yourself
Change a base in the DNA and watch the RNA update. Then count a specific base with dna.count("G"), and reverse the strand with dna[::-1] to see the other direction.
The common mistake
Mixing up the alphabets. DNA uses T; RNA uses U — never both. If you see a U in something labeled DNA, or a T in RNA, something transcribed wrong. Keeping the two four-letter alphabets straight is step one.
What it unlocks
Reading sequences leads to codons, the transcription and translation pipeline, and sequence analysis — all built on string methods.