Word Count Program In Python
Last Updated : Sep 14, 2023
IN - Python
In this article we will show you the solution of word count program in python, users can enter a text file or string into a Python word count programme to get an accurate count of the total number of words.
Developers can design a clear and dependable word count programme by utilising Python's vast range of built-in functions and string manipulation features.
To handle complex text formatting and filtering requirements, advanced methods like tokenization and regex patterns can be used.
This Python programme is a potent tool for textual data analysis that sheds light on the creation and organisation of documents.
Python's flexibility and efficiency enable users to complete a variety of text analysis tasks with ease, making the word count programme a valuable tool for anyone working with huge amounts of textual data.
Now move to the concept of word count program in python.
Step By Step Guide On Word Count Program In Python :-
def word_count(text): # Remove leading and trailing whitespaces text = text.strip() words = text.split() # Count the number of words count = len(words) return count def main(): text = input("Enter a text: ") # Call the word_count function to get the word count count = word_count(text) # Display the word count print("Word count:", count) if __name__ == "__main__": main()
- We define the word_count function at the beginning of the code to find out how many words are in a given text.
- As a parameter, it accepts a string of text. The strip() method is used to remove any leading and trailing whitespaces from the text at the beginning.
- This makes sure that extra spaces aren't counted in the word count.
- The changed text string is then divided into separate words using the split() technique.
- Split() separates the string by default, using whitespace as the delimiter.
- The words that result are kept in the words list.
- The count is then assigned to the variable count, and the length of the word list is determined using the len() function.
- The function then outputs the count value.
- The program's starting point is the main function.
- The input() function asks the user to enter a text.
- The variable text contains the user's input.
- To calculate the word count, the word_count function is used, supplying the text variable as an argument.
- The variable count is given the returned count.
- The user is then shown the word count by printing the string "Word count:" after the value of count.
- If the script is run directly, as opposed to being imported as a module, the main function will only be called, as ensured by the line if __name__ == "__main__".
As a result, we were able to learn how to use a Python word count program.
The Python word count program is a useful tool for analyzing textual data, as we also discovered.
Python's built-in functions & string manipulation abilities can be used by developers to make dependable and effective programs.
I hope this article on word count program in python helps you and the steps and method mentioned above are easy to follow and implement.