Home Content How to create random DNA sequences with Python

How to create random DNA sequences with Python

by Jack Simpson

Python’s random module makes it extremely easy to generate random DNA bases.

import random
dna = ["A","G","C","T"]
#output a random base
print(random.choice(dna))

Now to generate a specific number of random bases, all we have to do is use Python’s range function:

import random
dna = ["A","G","C","T"]
#initialise empty string
#this is where the bases will be added as they are generated
random_sequence=''
# We'll create a string of 100 random bases
for i in range(0,100):
    random_sequence+=random.choice(dna)
print(random_sequence)

Sign up to my newsletter

Sign up to receive the latest articles straight to your inbox

You may also like

Leave a Comment