Tutorials

Python 3 Code to Find Frequency of Each Word in String

In this tutorial, you will learn how to find the frequency of each word in a string and display it in Command-Line using Python 3 Code.

main.py

# Python code to find frequency of each word
def freq(str):

  # break the string into list of words
  str = str.split()    
  str2 = []

  # loop till string values present in list str
  for i in str:      

    # checking for the duplicacy
    if i not in str2:

      # insert value in str2
      str2.append(i)
      
  for i in range(0, len(str2)):

    # count the frequency of each word(present
    # in str2) in str and print
    print('Frequency of', str2[i], 'is :', str.count(str2[i]))  

def main():
  str ='apple mango apple orange orange apple guava mango mango'
  freq(str)          

if __name__=="__main__":
  main()       # call main function
Furqan

Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.

Recent Posts

MiniMax-M1 vs GPT-4o vs Claude 3 Opus vs LLaMA 3 Benchmarks

MiniMax-M1 is a new open-weight large language model (456 B parameters, ~46 B active) built with hybrid…

June 22, 2025

How to Use Husky with npm to Manage Git Hooks

Managing Git hooks manually can quickly become tedious and error-prone—especially in fast-moving JavaScript or Node.js…

June 22, 2025

How to Use Lefthook with npm to Manage Git Hooks

Git hooks help teams enforce code quality by automating checks at key stages like commits…

June 22, 2025

Lefthook vs Husky: Which Git Hooks Tool is Better? [2025]

Choosing the right Git hooks manager directly impacts code quality, developer experience, and CI/CD performance.…

June 22, 2025

Llama 3.1 vs GPT-4 Benchmarks

We evaluated the performance of Llama 3.1 vs GPT-4 models on over 150 benchmark datasets…

July 24, 2024

Transforming Manufacturing with Industrial IoT Solutions and Machine Learning

The manufacturing industry is undergoing a significant transformation with the advent of Industrial IoT Solutions.…

July 6, 2024