bodacydo bodacydo - 2 months ago 20
Python Question

How to check if text is "empty" (spaces, tabs, newlines) in Python?

How can I test if the string is empty in Python?

For example,

"<space><space><space>"
is empty, so is

"<space><tab><space><newline><space>"
, so is

"<newline><newline><newline><tab><newline>"
, etc.

Answer Source
yourString.isspace()

"Return true if there are only whitespace characters in the string and there is at least one character, false otherwise."

Combine that with a special case for handling the empty string.

Alternatively, you could use

strippedString = yourString.strip()

And then check if strippedString is empty.