python - Comment vérifier si la chaîne est vide

Mots clés : pythonstringcomparison-operatorspython

meilleur 5 Réponses python - Comment vérifier si la chaîne est vide

vote vote

94

if not myString: 
vote vote

80

if not some_string: 
if some_string: 
vote vote

75

if not my_string: 
 >>> bool("")  False  >>> bool("   ")  True  >>> bool("   ".strip())  False 
vote vote

67

def isBlank (myString):     if myString and myString.strip():         #myString is not None AND myString is not empty or blank         return False     #myString is None OR myString is empty or blank     return True 
def isNotBlank (myString):     if myString and myString.strip():         #myString is not None AND myString is not empty or blank         return True     #myString is None OR myString is empty or blank     return False 
def isBlank (myString):     return not (myString and myString.strip())  def isNotBlank (myString):     return bool(myString and myString.strip()) 
vote vote

55

def is_not_blank(s):     return bool(s and not s.isspace()) 
print is_not_blank("")    # False print is_not_blank("   ") # False print is_not_blank("ok")  # True print is_not_blank(None)  # False 

Questions similaires