Count Number Of Words In A String Python
Last Updated : Mar 11, 2024
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
- Initialize the quote variable with a string in it.
- On the next line of the code, we have initialized another variable name, listOfWords.
- The split() function with a separator(' ') will give us a list of all the words in the 'quote' string.
- 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.
- In the next line, we initialized another variable named length.
- We have used the len() function, which will give us the length of the list 'listOfWords'.
- 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.