How to Count the Number of A, T, C, and G Characters in a Sequence in Python?

Estimated read time 2 min read

To count the number of A, T, C, and G characters in a sequence in Python, you can iterate over the characters in the sequence and count the occurrences of each character. Here’s an example code snippet:

sequence = "ATCGCGATCGATGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTAGCTA"

counts = {"A": 0, "T": 0, "C": 0, "G": 0}

for char in sequence:
    if char in counts:
        counts[char] += 1

print("Counts:", counts)

In the above code snippet, we start by defining the DNA sequence as a string called sequence. We then create a dictionary called counts to store the counts for each character.

Inside the for loop, we iterate over each character in the sequence using a for loop. For each character, we check whether it is one of the four DNA bases (A, T, C, or G) by using an if statement. If the character is one of the four bases, we increment the count for that base in the counts dictionary.

Finally, we print the counts dictionary to show the number of occurrences of each base in the sequence.

Note that the above code snippet assumes that the sequence contains only valid DNA bases. If the sequence contains other characters or special characters, you may need to modify the code to exclude those characters from the counts.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply