How do I read two strings and check the number of occurrences of the second string in the first?
Example: If the first string entered is "abracadabra"
and the second "bra"
, then the number of occurrences will be 2 .
How do I read two strings and check the number of occurrences of the second string in the first?
Example: If the first string entered is "abracadabra"
and the second "bra"
, then the number of occurrences will be 2 .
Use str.count:
>>> nStr = 'abracadabra'
>>> nStr.count('bra')
Source - Determining how many times a substring occurs in a string in Python
frase = str(input("Digite a frase a se analisada: "))
palavra = str(input("Ocorrências da palavra/letra: "))
resultado = frase.count(palavra)
print(f"Na frase |{frase}|, a palavra/letra |{palavra}| apareceu {resultado} vezes")