To compute the count of occurrences of a substring within a larger string in Python, you can use the count()
method. Here’s an example of computing the count of occurrences of the substring “AAAAA” within the string “AACAAGCTGATAAACATTTAAAGAG”:
string = "AACAAGCTGATAAACATTTAAAGAG"
substring = "AAAAA"
count = string.count(substring)
print(count)
Output:
2
In this example, we use the count()
method of the string string
and pass the substring substring
as the argument. The count()
method returns the number of non-overlapping occurrences of the substring within the string.
The output shows that the substring “AAAAA” appears twice in the string “AACAAGCTGATAAACATTTAAAGAG”.
You can adapt this example to compute the count of any other substring within a given string by replacing the values of string
and substring
with your desired strings.
+ There are no comments
Add yours