Skip to content

Instantly share code, notes, and snippets.

@sp90
Created October 1, 2018 08:11
Show Gist options
  • Save sp90/e0c6b0b5fd9bef81dd1b96e038fdb670 to your computer and use it in GitHub Desktop.
Save sp90/e0c6b0b5fd9bef81dd1b96e038fdb670 to your computer and use it in GitHub Desktop.
Simple python function to count occurenances
# We need to count the occurance of words in a text
def count_word_in_text(text, word_to_count_by):
# We first have the text and then we run a "count" method on the text
# then we parse the word_to_count_by
return text.count(word_to_count_by)
# Now we add a string of text we want to count the occurances of a certain word in
# Secound paramater is the word we want to count
count_of_words = count_word_in_text("Hello world, its a great day in this world", "world")
# Prints 2 because the word world is present in the example twice
print(count_of_words); # 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment