String

Count the number of white space of the given string in Python

Count the number of white space of the given string in Python

In this article, we will discuss the concept of the Count the number of white spaces of the given string in Python

In this post, we are going to learn how to  count the number of spaces in  the given string in Python programming language

Count the number of while spaces

Code to count the white space in the string

count the white space in the given string using for loop

The program allows the user to enter a String and then it counts the number of white spaces in the given string using for loop in Python programing language

Program 1

str=input("Enter the string: \n")
spaces=0
for i in range(0,len(str)):
  if(str[i]==' '):
    spaces=spaces+1
print("The number of spaces are: ",spaces)

When the above code is executed, it produces the following result

Enter the string
This is the python language
The number of spaces is: 4

 

Approaches

  1. Declare a string variable str
  2. Declare and initialize integer variables as space=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A for loop is used to count space in the given string.
  6. Use an if condition to test if(str[i]==’  ‘), when the if statements is true,  The space becomes space + 1( space = space +1);
  7. When it is false, terminates the loop
  8. Finally, the program displays the total number of space of the given string

 

 

Count the number of while space of the given string using isspace()

The program allows the user to enter a String and then it counts the white space in the given string using isspace() built-in function in Python programing language

Program 2

str=input("Enter the string: ")
space=0
for i in str:
  if(i.isspace()):
    space=space+1
print("The number of blank space is: ",space)

When the above code is executed, it produces the following result

Enter the string: Python language supports OOP concept
The number of blank space is: 4

Approaches

  1. Declare a string variable str
  2. Declare and initialize integer variables as space=0;
  3. The user asked to enter a string
  4. The given string is stored in the variable str;
  5. A for loop is used to count space in the given string.
  6. Use an if condition to test if(i.isspace()):, when the if statements returns true, space becomes space + 1( space = space +1);
  7. When it is false, terminates the loop
  8. Finally, the program displays the total number of space of the given string

 

Suggested for you

for loop in Python language

while loop in Python language

 

 

Similar post

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 find the total number of vowels and consonants digits special characters and spaces

C program to find the total number of vowels and consonants digits special characters and spaces

C++ program to find the total number of vowels and consonants digits special characters and spaces

 

C++ code: Count Number of space of the given string
Java program: Find the frequency of each character in the string
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago