-1

so I'm having problem with finding the frequency of a word in a string. I'm trying to ask the user to input the string and the word. Let's say the user entered plopewdsplopewds and the word is "pewds" which is repeated twice. The string.split() method here won't work. I'm trying with it since hours and I couldn't find a decent working solution. The use of for didn't work in this case. If anyone has an idea please share it with me I have to do it today.

EDIT: I'm Trying to use a loop here. But I could only use it for characters:

text = input("text:\n> ")
char = input("character:\n> ")
def frequency(x):
  f=0
  for i in str(x):
    if (i == str(char)):
      f += 1
    else:
      f=f
  return f

print(frequency(text))
rgbxx
  • 13
  • 2
  • 2
    Possible duplicate of [Count the number occurrences of a character in a string](https://stackoverflow.com/questions/1155617/count-the-number-occurrences-of-a-character-in-a-string) – Sayse May 16 '18 at 07:05
  • 1
    You could either use a regex, or if you want to allow overlapping it would be more sufficient to use a loop – GalAbra May 16 '18 at 07:06
  • I'm trying to work with the loops in this exercise. – rgbxx May 16 '18 at 07:07
  • You should show what you've tried. – Sayse May 16 '18 at 07:07

1 Answers1

3

I hope you can use count method

sampleString = "plopewdsplopewds"

print(sampleString.count("pewds"))
Moinuddin Quadri
  • 43,657
  • 11
  • 92
  • 117
Venky1729
  • 67
  • 4
  • Thanks that is what I'm looking for. But I'm trying to go the hard way and use a loop here. – rgbxx May 16 '18 at 07:12