All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Count Number Of Words In A String Python

Last Updated : Mar 11, 2024

Count Number Of Words In A String Python

In this article we will show you the solution of count number of words in a string python, often, we need to perform various tasks on a string, which may include calculating the number of characters in a string or calculating the number of words in a String, or calculating the number of specific words in a String, etc.

In this article, we will find the count of the number of words in a String in Python.

To have a count of the number of words in a string in Python helps us in many operations like iteration, defining conditions for conditional statements, etc.

Step By Step Guide On Count Number Of Words In A String Python :-

One way to find the count of the number of words in a string will be to count the number of spaces in a given string and add one to it. This will give the total count of the number of words in a string in Python.

However in this article, we will use a different approach to find the count of the number of words in a string which is by using the built-in split() function and len() function.

As built-in functions, these functions can be called directly in the code and need not be defined first.

split() function :

Syntax: str.split(separator)

Use :-

The split() function splits the string by the specified separator. The split() function returns the list of all the substrings which will be formed after splitting using the separator.

For example, if we specified the separator as 's' then, the string characters will be split through 's', and all the substrings will be combined in a list.

Similarly, if we specify the separator as space(' '), the whole string is

separated into words combined in the list.

len() function

Syntax: len(object)

Use: The len() function gives the number of items in an object in Python.

Any object can be passed through the len() function, such as a string, list, array, dictionary, tuple, etc.

quote = "TalkersCode is the best site to learn programming concepts."
listOfWords = quote.split()
print(listOfWords)
length = len(listOfWords)
print(length)

#Code Output
['TalkersCode', 'is', 'the', 'best', 'site', 'to', 'learn', 'programming', 'concepts.']
9
  1. Initialize the quote variable with a string in it.
  2. On the next line of the code, we have initialized another variable name, listOfWords.
  3. The split() function with a separator(' ') will give us a list of all the words in the 'quote' string.
  4. To clarify the concept, we have printed the listOfWords variable, and in the output it can be seen a list of all the words from the 'quote' string is obtained.
  5. In the next line, we initialized another variable named length.
  6. We have used the len() function, which will give us the length of the list 'listOfWords'.
  7. Finally, we have printed the length variable showing the count of the number of words in a string in Python, which is 9 in this example.

Conclusion :-

Thus, this is one of the easiest methods to find the count of the number of words in a string in Python. Try using this method with various examples.

I hope this article on count number of words in a string python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪