;

# Python Program to Count the Total Number of words in a Given String

###### Tutorialsrack 27/04/2020 Python

In this python program, we will learn how to count the total number of words in a given string. For counting the total number of words, we use `for` and `while` loop and `len()` function to count the length of the string.

Here is the source code of the program to count the total number of words in a given string.

Program 1: Python Program to Count the Total Number of words in a Given String Using For Loop with Range

##### Python Program to Count the Total Number of words in a Given String Using For Loop with Range
``````# Python Program to Count the Total Number of words in a Given String Using For Loop with Range

# Take the Input from the User
str1 = input("Enter The String: ")
total = 1

for i in range(len(str1)):
if(str1[i] == ' ' or str1 == '\n' or str1 == '\t'):
total = total + 1

print("Total Number of Words in this String = ", total)
``````
##### Output

Enter The String: Tutorials Rack is an Online Learning Website

Total Number of Words in this String =  7

### Program 2: Python Program to Count the Total Number of words in a Given String Using While Loop

##### Python Program to Count the Total Number of words in a Given String Using While Loop
``````# Python Program to Count the Total Number of words in a Given String Using While Loop

# Take the Input from the User
str1 = input("Enter the String: ")
total = 1
i = 0

while(i < len(str1)):
if(str1[i] == ' ' or str1 == '\n' or str1 == '\t'):
total = total + 1
i = i + 1

print("Total Number of Words in this String = ", total)
``````
##### Output

Enter the String: Tutorials Rack is an Online Learning Website

Total Number of Words in this String =  7

### Program 3: Python Program to Count the Total Number of words in a Given String Using a Function

Python Program to Count the Total Number of words in a Given String Using a Function

``````# Python Program to Count the Total Number of words in a Given String Using a Function

# Defining a Function
def Count_Total_Words(str1):
total = 1
for i in range(len(str1)):
if(str1[i] == ' ' or str1 == '\n' or str1 == '\t'):
total = total + 1

# Take the Input From the User
string = input("Enter The String: ")
Total_Words_Count = Count_Total_Words(string)
print("Total Number of Words in this String = ", Total_Words_Count)
``````
##### Output

Enter The String: Tutorials Rack is an Online Learning Website

Total Number of Words in this String =  7