- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the most elegant way to check if the string is empty in Python?
In Python, an empty string is a string with no characters in it. It is represented by a pair of single quotes '' or a pair of double quotes "". An empty string is different from a null value, which is a special value representing the absence of any object. An empty string can be used in various ways, such as initializing a string variable or checking if a string is empty.
The most elegant way to check if a string is empty in Python is to simply use the Boolean evaluation of the string. Here are some examples:
Using Boolean evaluation
Example
In this example, we define an empty string and then check if it is empty by evaluating the Boolean value of the string using the not operator. Since the string is empty, the Boolean value is False, so the if statement evaluates to True and prints "The string is empty!".
# define an empty string my_string = "" # check if the string is empty using boolean evaluation if not my_string: print("The string is empty!") else: print("The string is not empty.")
Output
The string is empty!
Using Boolean evaluation
Example
In this example, we define a non−empty string and then check if it is empty using Boolean evaluation. Since the string is not empty, the Boolean value is True, so the else statement is executed and prints "The string is not empty.".
# define a non-empty string my_string = "lorem ipsum" # check if the string is empty using boolean evaluation if not my_string: print("The string is empty!") else: print("The string is not empty.")
Output
The string is not empty.
Using the len() function
Example
In this example, we define an empty string and then check if it is empty by using the len() function to check the length of the string. Since the length of the string is 0, the if statement evaluates to True and prints "The string is empty!". This method is slightly less elegant than using boolean evaluation, but is still commonly used.
# define an empty string my_string = "" # check if the string is empty using len() if len(my_string) == 0: print("The string is empty!") else: print("The string is not empty.")
Output
The string is empty!
Using the len() function
Example
Here, we're using the len() function to find the length of the string. If the length is zero, it means the string is empty.
my_string = "Foo baz" if len(my_string) == 0: print("The string is empty") else: print("The string is not empty")
Output
The string is not empty
Using the not operator
Example
In this example, we're using the not operator to check if the string is empty. An empty string is considered False in Python, so when we use the not operator, we get True if the string is empty.
my_string = "" if not my_string: print("The string is empty") else: print("The string is not empty")
Output
The string is empty
Using the string method isspace()
Example
In this example, we're using the string method isspace(). This method returns True if the string contains only whitespace characters (such as spaces, tabs, and newlines), and False otherwise. If the string is empty, it contains no non−whitespace characters, so isspace() returns True, and we know the string is empty.
my_string = " " if my_string.isspace(): print("The string is empty") else: print("The string is not empty")
Output
The string is empty