To check an empty string in Python, use not operator, or use len() function, or use not + string.strip() or use not + string.isspace() ways.
String data type in Python is immutable; that means you can not change a string once defined. String with spaces is actually an empty string but has non-zero size. So, when you check the empty string with len() or not operator, it counts space as a character in the string, and hence it won’t count a string with space as an empty string.
To check an empty string in Python, you can use one of the following methods.
The not operator in Python checks the string with just spaces to be non-empty, which should not practically be True.
## app.py strA = "" ## checking if string is empty print("Check if the strA is empty : ", end="") if(not strA): print("The string is empty") else: print("No it is not empty")
Check if the strA is empty : The string is empty
The string is empty, and one thing to note that we did not even put the space in the string. That is why it returns as empty. If we put a space in the string, it will not count as empty, and the not operator returns False.
## app.py strA = " " ## checking if string with space is empty print("Check if the strA with space is empty : ", end="") if(not strA): print("The string is empty") else: print("No it is not empty")
Guide to Python Programming Language
Python is an interpreted, high-level, powerful general-purpose programming language. You may ask, Python’s a snake right? and Why is this programming language named after it?
Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.
Python Programming Tutorials For Beginners
Today you're going to learn how to use Python programming in a way that can ultimately save a lot of space on your drive by removing all the duplicates. We gonna use Python OS remove( ) method to remove the duplicates on our drive. Well, that's simple you just call remove ( ) with a parameter of the name of the file you wanna remove done.