Warning: session_start(): open(/tmp/sess_7d1f67df14cdebaedc8f3464b6bde316, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check If A Line Is Empty In Python - LearnShareIT

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!

Leave a Reply

Your email address will not be published. Required fields are marked *