---
title: "How to Count and Print Blank Spaces in Python"
description: "Two Python methods that show you how to count blank spaces in a text file"
url: https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/
date: 2023-05-28
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2023/05/python-count.jpg?fit=2240%2C1260&ssl=1
categories: ["Python"]
tags: ["Counting blank spaces"]
type: post
lang: en
---

# How to Count and Print Blank Spaces in Python

Here are two methods that show [how to](https://srinimf.com/2022/12/26/these-python-datetime-classes-developers-should-know/) count and print blank spaces in Python. The input is a text file. The program looks for blank spaces and counts them. In the end, it prints the number of blank spaces.

Related[Pass by Value and Pass by Reference in Python](https://srinimf.com/2022/05/23/python-pass-by-value-vs-pass-by-reference-simple-example/)

ON THIS PAGE

1. [How to count blank spaces using for loop](https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/#how-to-count-blank-spaces-using-for-loop)
2. [How to count blank spaces using while loop](https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/#how-to-count-blank-spaces-using-while-loop)

## How to count blank spaces using for loop

The input file is ***my_text***. The count gives you the blank spaces count.

```
my_text="my name is srini"
count=0
for i in my_text:
    if i == ' ':
        count += 1
        
print(count)
```

Output

```
3
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Advertisements

## How to count blank spaces using while loop

Using a while loop how to count blank spaces explained in the below python program.

```
my_text = "my name is srini"
i=0
count=0
while i < len(my_text):
    if my_text[i] == ' ':
        count += 1
    i += 1
print(count)
```

Output

```
3
** Process exited - Return Code: 0 **
Press Enter to exit terminal
```

Related

- [Python Strings: Program to Replace Second Occurrence with Input](https://srinimf.com/2023/11/15/get-and-replace-index-value-python-string/)DateNovember 15, 2023
- [3 Popular Ways to Handle DB2 VARCHAR Field in COBOL](https://srinimf.com/2014/08/14/3-ways-to-handle-db2-varchar-filed-in-cobol/)DateAugust 14, 2014
- [7 Daily Use Python String Functions](https://srinimf.com/2022/02/19/daily-use-python-string-functions/)DateFebruary 19, 2022

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2023%2F05%2F28%2Fhow-to-print-and-count-blank-spaces-in-python%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2023%2F05%2Fpython-count.jpg%3Ffit%3D1200%252C675%26ssl%3D1&description=How%20to%20Count%20and%20Print%20Blank%20Spaces%20in%20Python)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2023/05/28/how-to-print-and-count-blank-spaces-in-python/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Count%20and%20Print%20Blank%20Spaces%20in%20Python&body=https%3A%2F%2Fsrinimf.com%2F2023%2F05%2F28%2Fhow-to-print-and-count-blank-spaces-in-python%2F&share=email)
-

### Like this:

Like Loading…
