
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Count words in a sentence in Python program
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a string we need to count the number of words in the string
Approach 1 − Using split() function
Split function breaks the string into a list iterable with space as a delimiter. if the split() function is used without specifying the delimiter space is allocated as a default delimiter.
Example
test_string = "Tutorials point is a learning platform" #original string print ("The original string is : " + test_string) # using split() function res = len(test_string.split()) # total no of words print ("The number of words in string are : " + str(res))
Output
The original string is : Tutorials point is a learning platform The number of words in string are : 6
Approach 2 − Using regex module
Here findall() function is used to count the number of words in the sentence available in a regex module.
Example
import re test_string = "Tutorials point is a learning platform" # original string print ("The original string is : " + test_string) # using regex (findall()) function res = len(re.findall(r'\w+', test_string)) # total no of words print ("The number of words in string are : " + str(res))
Output
The original string is: Tutorials point is a learning platform The number of words in string is: 6
Approach 3 − Using sum()+ strip()+ split() function
Here we first check all the words in the given sentence and add them using the sum() function.
Example
import string test_string = "Tutorials point is a learning platform" # printing original string print ("The original string is: " + test_string) # using sum() + strip() + split() function res = sum([i.strip(string.punctuation).isalpha() for i in test_string.split()]) # no of words print ("The number of words in string are : " + str(res))
Output
The original string is : Tutorials point is a learning platform The number of words in string are : 6
All the variables are declared in the local scope and their references are seen in the figure above.
Conclusion
In this article, we have learned how to count the number of words in the sentence.
- Related Articles
- Python program to count words in a sentence
- Count palindrome words in a sentence in C++
- Python program to sort Palindrome Words in a Sentence
- Python program to Count words in a given string?
- Python program to sort out words of the sentence in ascending order
- Python - Generate all possible permutations of words in a Sentence
- Rearrange Words in a Sentence in C++
- C program to count a letter repeated in a sentence.
- Java program to sort words of sentence in ascending order
- Program to reverse a sentence words stored as character array in C++
- Python - Check if given words appear together in a list of sentence
- Counting number of words in a sentence in JavaScript
- Java program to swap first and last characters of words in a sentence
- Python program to count distinct words and count frequency of them
- C# program to remove all duplicates words from a given sentence
