How do you count words in a for loop in python?

Python program to count the total number of words in the given string

Python program to count the total number of words in the given string

Contents

  • 1 Python program to count the total number of words in the given string
      • 1.0.1 Program to count the total number of words using for loop
      • 1.0.2 Program to count the total number of words using while loop
      • 1.0.3 Program to count the total number of words using the split() function
    • 1.1 Related posts:

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

How do you count words in a for loop in python?
Count words of the string

Program to count the total number of words using for loop

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

  1. Declare and initialize an integer variable as tot=1;
  2. The user asked to enter a string to count words
  3. A for-loop is used to count the total words of the given string
  4. Use an if condition to test if(str1[i]==’ ‘ or str1 == ‘/n’ or str1 == ‘\t’):, if it is true,  tot becomes tot+ 1(tot=tot+1)
  5. Finally, the program displays the total number of the words using the tot variable

Program to count the total number of words using while loop

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

  1. Declare and initialize an integer variable as tot=1,i=0;
  2. The user asked to enter a string to count words
  3. A while-loop is used to count the total words of the given string
  4. Use an if condition to test if(str1[i]==’ ‘ or str1 == ‘/n’ or str1 == ‘\t’):, if it is true,  tot becomes tot+ 1(tot=tot+1)
  5. i is incremented by 1
  6. Finally, the program displays the total number of the words using the tot variable

Program to count the total number of words using the split() function

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

For loop in Python language

While loop in Python language

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

How do you count words in a for loop?

Program to count the total number of words using for loop.
Declare and initialize an integer variable as tot=1;.
The user asked to enter a string to count words..
A for-loop is used to count the total words of the given string..

How do you do a for loop count in Python?

Use the enumerate() function to count in a for loop, e.g. for index, item in enumerate(my_list): . The function takes an iterable and returns an object containing tuples, where the first element is the index, and the second - the item. Copied!

How do you count specific words in Python?

Using the count() Function The "standard" way (no external libraries) to get the count of word occurrences in a list is by using the list object's count() function. The count() method is a built-in function that takes an element as its only argument and returns the number of times that element appears in the list.