
- 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
Python program to Count words in a given string?
Lets suppose we have a ‘string’ and the ‘word’ and we need to find the count of occurence of this word in our string using python. This is what we are going to do in this section, count the number of word in a given string and print it.
Count the number of words in a given string
Method 1: Using for loop
#Method 1: Using for loop
test_stirng = input("String to search is : ") total = 1 for i in range(len(test_stirng)): if(test_stirng[i] == ' ' or test_stirng == '\n' or test_stirng == '\t'): total = total + 1 print("Total Number of Words in our input string is: ", total)
Result
String to search is : Python is a high level language. Python is interpreted language. Python is general-purpose programming language Total Number of Words in our input string is: 16
#Method 2: Using while loop
test_stirng = input("String to search is : ") total = 1 i = 0 while(i < len(test_stirng)): if(test_stirng[i] == ' ' or test_stirng == '\n' or test_stirng == '\t'): total = total + 1 i +=1 print("Total Number of Words in our input string is: ", total)
Result
String to search is : Python is a high level language. Python is interpreted language. Python is general-purpose programming language Total Number of Words in our input string is: 16
#Method 3: Using function
def Count_words(test_string): word_count = 1 for i in range(len(test_string)): if(test_string[i] == ' ' or test_string == '\n' or test_string == '\t'): word_count += 1 return word_count test_string = input("String to search is :") total = Count_words(test_string) print("Total Number of Words in our input string is: ", total)
Result
String to search is :Python is a high level language. Python is interpreted language. Python is general-purpose programming language Total Number of Words in our input string is: 16
Above are couple of other ways too, to find the number of words in the string entered by the user.
- Related Articles
- Java program to count words in a given string
- C# program to Count words in a given string
- Count words in a given string in C++
- Python program to count words in a sentence
- How to count a number of words in given string in JavaScript?
- Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary
- Count words in a sentence in Python program
- Java Program to count the number of words in a String
- C# program to count the number of words in a string
- Reverse words in a given String in Python
- Python program to count number of vowels using set in a given string
- Python program to print even length words in a string
- Python program to count distinct words and count frequency of them
- Python program to count the number of vowels using set in a given string
- Python program to count the number of vowels using sets in a given string

Advertisements