- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to count words in a sentence
In this article, we will learn about the solution and approach to solve the given problem statement.
Problem statement
We are given a sentence, we need to count the number of words in the sentence
Here we will be discussing two approaches −
Approach 1 − Using split() function
Example
test_string = "Tutorials point " res = len(test_string.split()) print ("The number of words in string are : " + str(res))
Output
The number of words in string are : 2
Approach 2 − Using strip() & isalpha() function
Example
import string test_string = "Tutorials point " res = sum([i.strip(string.punctuation).isalpha() for i in test_string.split()]) print ("The number of words in string are : " + str(res))
Output
The number of words in string are : 2
Conclusion
In this article, we learnt about the approach to count words in a sentence.
- Related Articles
- Count words in a sentence in Python program
- Python program to sort Palindrome Words in a Sentence
- Count palindrome words in a sentence in C++
- Python program to Count words in a given string?
- Python program to sort out words of the sentence in ascending order
- 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 program to count distinct words and count frequency of them
- C# program to remove all duplicates words from a given sentence
- Java program to remove all duplicates words from a given sentence
- Python - Generate all possible permutations of words in a Sentence
- Java program to swap first and last characters of words in a sentence
- Rearrange Words in a Sentence in C++
- C# program to Count words in a given string

Advertisements