# How To Check If A Line Is Empty In Python

Sometimes, when you print a line in Python, it looks like an empty String in Python. In some cases, it is not correct. Usually, it is a whitespace, a next-line sign, etc. To check if a line is Empty in Python, you can use the equal sign, in operator, or the strip() function. Follow the explanation below to understand better.

## Check If A Line Is Empty In Python

### Using the equal sign

To check if a line is Empty in Python, you can use the equal sign. Simply, you check if the line is an empty String or not. If True, the line is empty. Otherwise, it is not empty.

Look at the example below.

def is_empty_line(line=str()):
# Check if a line is empty or not with the equal sign.
if line == '':
print('Empty Line!')
else:
print('Not!')

# Check whether a line is empty or not.
line = ''
is_empty_line(line)

# Try with other Strings.
line = '\n'
is_empty_line(line)

line = ' \r'
is_empty_line(line)

Output

Empty Line!
Not!
Not!

### Using the in operator

Besides the equal sign, you can use the in operator to check if a line is Empty in Python. Typically, people usually mistake the empty line with the white space, ‘\n’, ‘\r’ in Python. So you can create a list that contains these values and check if a line is in it or not.

Look at the example below.

def is_empty_line(line=str()):
# The typical mistake.
mistake_list = ['\n', '\r', ' ', '\t']

# Check if a line is Empty with the in operator.
if line in mistake_list:
print('Not!')
else:
print('Empty Line!')

# Check whether a line is empty or not.
line = ''
is_empty_line(line)

# Try with other Strings.
line = '\n'
is_empty_line(line)

line = '\r'
is_empty_line(line)

Output

Empty Line!
Not!
Not!

### Using the strip() function

In addition, you can use the strip() function to check if a line is empty or not. If the strip() function returns True, the line is not empty. Otherwise, it is empty.

Look at the example below.

def is_empty_line(line=str()):
# Check if a line is Empty with the strip() function.
if line.strip() != line:
print('Not!')
else:
print('Empty Line!')

# Check whether a line is empty or not.
line = ''
is_empty_line(line)

# Try with other Strings.
line = '\n'
is_empty_line(line)

line = '\r'
is_empty_line(line)

Output

Empty Line!
Not!
Not!

## Summary

We have shown you how to check if a line is Empty in Python in 3 ways. From our point of view, you should use the equal sign or strip() function because you may miss some cases if you use the in operator. Hope you have an enjoyable learning experience. Thanks!