Python check if line is empty or whitespace

Check if String contains only Spaces #

To check if a string contains only spaces, call the trim() method on the string and check if the length of the result is equal to 0. If the string has a length of 0 after calling the trim method, then the string contains only spaces.

Copied!

function onlySpaces(str) { return str.trim().length === 0; } console.log(onlySpaces(' ')); // πŸ‘‰οΈ true console.log(onlySpaces(' 123 ')); // πŸ‘‰οΈ false console.log(onlySpaces('hello ')); // πŸ‘‰οΈ false console.log(onlySpaces('')); // πŸ‘‰οΈ true

The String.trim method removes the whitespace characters from both ends of a string.

We check if the result of calling the method on the string returns a string of length 0. If it does, we either have a string that contains only spaces or an empty string.

The trim method does not change the original string, it returns a new string. Strings are immutable in JavaScript.

An alternative, but also very common approach is to use a regular expression.

Check if String contains only with Regular Expression #

To check if a string contains only spaces, use the test() method with the following regular expression /^\s*$/. The test method will return true if the string contains only spaces and false otherwise.

Copied!

function onlySpaces(str) { return /^\s*$/.test(str); } console.log(onlySpaces(' ')); // πŸ‘‰οΈ true console.log(onlySpaces(' test ')); // πŸ‘‰οΈ false console.log(onlySpaces('hello ')); // πŸ‘‰οΈ false console.log(onlySpaces('')); // πŸ‘‰οΈ true

We used the RegExp.test method to check if a string contains only spaces.

The method returns true if the regular expression is matched in the string and false otherwise.

The forward slashes / / mark the beginning and end of the regular expression.

The caret ^ matches the beginning of the input and the dollar sign $ matches the end of the input.

The \s character matches a single space, tab or newline.

The asterisk * character matches the preceding item (space) 0 or more times.

If you ever need help reading a regular expression, bookmark this regex cheatsheet from MDN.

Which approach you pick is a matter of personal preference. I'd go with the trim() method as I find it easier to read and more beginner friendly.

Further Reading #

  • Check if String contains Whitespace in JavaScript
  • Check if String contains only Letters in JavaScript
  • Check if String contains only Latin Letters in JavaScript
  • Check if String contains only Digits in JavaScript
  • Check if String contains only Letters and Numbers in JS
  • Check if String contains only Letters and Spaces in JS
  • Ignore case of startsWith, endsWith Methods in JavaScript

PythonServer Side ProgrammingProgramming




Python check if line is empty or whitespace

Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

Mohammad Nauman

More Detail

Python check if line is empty or whitespace

Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

MANAS DASGUPTA

More Detail

Python check if line is empty or whitespace

Practical Data Science using Python

22 Lectures 6 hours

MANAS DASGUPTA

More Detail

The string can be checked by checking for occurrences of only whitespace characters. We can check if a string contains only whitespace characters using 2 methods. First is using method isspace(). 

example

print('Hello world'.isspace())
print('         '.isspace())

Output

False
True

You can also use regexes for the same result. For matching only whitespace, we can call the re.match(regex, string) using the regex metacharacter \s as follows: "^\s*$". 

example

import re
print(bool(re.match('^\s+$', '  abc')))
print(bool(re.match('^\s+$', '          ')))

Output

False
True

Python check if line is empty or whitespace

Manogna

Updated on 13-Feb-2020 09:59:53

  • Related Questions & Answers
  • How to strip spaces/tabs/newlines using Python regular expression?
  • How to remove tabs and newlines using Python regular expression?
  • Python - Check if dictionary is empty
  • How to check if a list is empty in Python?
  • How to check if a text field is empty or not in swift?
  • How to expand tabs in string to multiple spaces in Python?
  • How to check if android editText is empty?
  • Python Pandas - Check if an interval is empty
  • How to check if String is empty in Java?
  • How to check if PSCustomObject is empty in PowerShell?
  • How do i match just spaces and newlines with Python regex?
  • How can I replace newlines with spaces in JavaScript?
  • How to check if a C# list is empty?
  • How to check if a Laravel collection is empty?
  • Check if value is empty in JavaScript

Previous Page Print Page Next Page  

Advertisements

How do I check if a string is empty or only spaces in Python?

Python String isspace() method returns β€œTrue” if all characters in the string are whitespace characters, Otherwise, It returns β€œFalse”. This function is used to check if the argument contains all whitespace characters, such as: ' ' – Space.

How do you check if a line is blank in Python?

Use an if-statement to check if a line is empty Call file. readlines() to return all lines in file . Use a for-loop to iterate through each line. For each line, use an if-statement with the syntax if line == "\n" to check if line contains only a newline character.

How do I check if a string contains whitespace?

Approach:.
Get the String to be checked in str..
We can use the trim() method of String class to remove the leading whitespaces in the string. ... .
Then we can use the isEmpty() method of String class to check if the resultant string is empty or not. ... .
Combine the use of both methods using Method Chaining..

How do you handle blank space in Python?

strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.