I'm defining a function that counts the number of times XML tags are present in a string, namely <> and </>. Although I'm able to 'match' the strings, I'm unable to quantify those number of matches to return a numeric count. This is my function:
def tag_count(the_string):
count = 0
for element in the_string:
if '<>' in the_string:
count += 1
return count
elif '</>' in the_string:
count += 1
return count
The problem is that for strings that have both <> and </>, count should return the number of times these tags are matched whereas my function is only returning count as 1 because of the elif condition. I tried inserting and in the 3rd line but that gives me an error. How do I sum the number of times these tags are matched?