In this article, we will discuss the concept of Python program to count the total number of words in the given string
In this post, we are going to learn how to count the total number of words in the given string in Python programming language
The program allows the user to enter a string thereafter It counts the words of the string using for loop in Python language
Program 1
#Python program to count the total number of words in the given string str1=input("Please Enter a string as you wish\n") tot=1; for i in range(len(str1)): if(str1[i]==' ' or str1 == '/n' or str1 == '\t'): tot=tot+1 print("Total number of words in the given string= ",tot)
When te above code is executed, it produces the following result
Please Enter a string as you wish Python programming language Total number of words in the given string= 3
Approach
The program allows the user to enter a string thereafter It counts the words of the string using while loop in Python language
Program 2
#python program to count total number of words in a string str1=input("Please enter a string as you wish: ") tot=1 i=0 while(i<len(str1)): if(str1[i]==' ' or str1 == '/n' or str1 == '\t'): tot=tot+1 i=i+1 print("Total number of words in the given string= ",tot)
When te above code is executed, it produces the following result
Please enter a string as you wish: Python supports oop concept Total number of words in the given string= 4
Approach
Program 3
#python program to count total number of words in a string str1=input("Enter a string as you wish\n") split_str1=str1.split() NumberOfWords=len(split_str1) print("Total words:{}".format(NumberOfWords))
When te above code is executed, it produces the following result
Enter a string as you wish Python is an easy language Total words:5
Suggested for you
Similar post
Java program to count the total number of characters in the given string
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of words in the given string
C program to count the total number of words in the given string
C++ program to count the total number of words in the given string
Explanation of one dimensional array In this post, we will discuss the concept of "Explanation…
Python program to calculate the sum of odd and even numbers in a list In…
Python code to Calculate sum of odd and even in a list In this tutorial,…
How to find reverse number using method In this article, we will discuss the concept…
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…