How to check if a Line is Empty in Python

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
4 min

banner

# Check if a line is Empty in Python

To check if a line is empty:

  1. Open the file in reading mode.
  2. Use the file.readlines() method to get the lines in the file.
  3. Use the line.strip() method to check if the line is empty.
main.py
# โœ… check if a line is NOT empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip(): print('The line is NOT empty ->', line) else: print('The line is empty') print('------------------------------------------') # โœ… check if a line is empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip() == "": print('The line is empty') else: print('The line is NOT empty', line)

checking if line is not empty

The code for this article is available on GitHub

We used the with statement to open the file in reading mode.

The statement automatically takes care of closing the file for us.

We used the f.readlines() method to get a list of the lines in the file.

The str.strip method returns a copy of the string with the leading and trailing whitespace (including newline characters) removed.

main.py
print(repr('\n'.strip())) # ๐Ÿ‘‰๏ธ '' print(repr(' '.strip())) # ๐Ÿ‘‰๏ธ ''

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

# Checking if a line in a file isn't empty

The first example checks if each line isn't empty.

main.py
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip(): print('The line is NOT empty ->', line) else: print('The line is empty')
The code for this article is available on GitHub

If the str.strip() method returns a truthy value (not an empty string), then the line isn't empty.

To check if a line is empty, compare the result of calling str.strip() with an empty string.

main.py
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line.strip() == "": print('The line is empty') else: print('The line is NOT empty', line)

checking if line is empty

If the str.strip() method returns an empty string, then the line is empty.

This approach returns True if the line contains whitespace characters or newlines.

If you only want to check for newline characters, use the in operator.

# Check if a line is Empty using the in operator

This is a three-step process:

  1. Open the file in reading mode.
  2. Use the file.readlines() method to get the lines in the file.
  3. Use the in operator to check if the line is empty.
main.py
# โœ… check if a line is NOT empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line not in ['\n', '\r', '\r\n']: print('The line is NOT empty ->', line) else: print('The line is empty') print('------------------------------------------') # โœ… check if a line is empty with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line in ['\n', '\r', '\r\n']: print('The line is empty') else: print('The line is NOT empty', line)

checking if line is not empty using in operator

The code for this article is available on GitHub

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise, it evaluates to False.

x not in l returns the negation of x in l.

The first example checks if each line in the file isn't empty.

If the string stored in the line variable is not equal to \n, \r and \r\n, then it's not an empty line.

The newline characters are:

  • \n for POSIX style encoded files
  • \r\n for Windows-style encoded files
  • \r for old Mac encoded files

To check if the line is empty, use the in operator instead.

main.py
with open('example.txt', 'r', encoding="utf-8") as f: lines = f.readlines() for line in lines: if line in ['\n', '\r', '\r\n']: print('The line is empty') else: print('The line is NOT empty', line)

checking if line is empty using in operator

The code for this article is available on GitHub

If the line variable is one of \n, \r or \r\n, then it's an empty line.

I've also written an article on how to check if user input is empty.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev