Search code examples
countspacespecificationsletter

Counting spaces after a specific letter


I want to count the spaces after the letter T or t.

text:

Mr oh winding it enjoyed by between. The servants securing material goodness her. \
Saw principles themselves ten are possession. So endeavor to continue

Solution

  • If you want to use regular expressions, then [Tt] is the pattern for "a T or t followed by a space". To count the number of occurrences of this pattern in a text, you can write:

    len(re.findall('[Tt] ', text))
    

    If you don't want to use regular expressions, then you can write:

    text.count('T ') + text.count('t ')