How to count occurrences of a substring in a Python string?

Asked

Viewed 7,052 times

3

How do I read two strings and check the number of occurrences of the second string in the first?

Example: If the first string typed is "abracadabra" and the second "bra", then the number of occurrences will be 2.

2 answers

7

0

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")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.