Pour vérifier si une chaîne de caractères est vide (ou composée d'espaces uniquement, ou de tabulations) sous python on peut soit utiliser les méthodes len() et strip() ou isspace(). Illustration pour une chaine vide:
>>> s = ''
>>> if len(s) == 0:
... print "s is empty"
...
s is empty
ou
>>> s = ''
>>> if not s:
... print "s is empty"
...
s is empty
pour une chaîne de caractères composée d'espaces uniquement:
>>> s = ' '
>>> if len(s.strip()) == 0:
... print "s is empty"
...
s is empty
ou
>>> s = ' '
>>> s.isspace()
True
pour une chaîne de caractères avec des tabulations uniquement:
>>> s = ' '
>>> s
'\t'
>>> s.isspace()
True
Références
Liens | Site |
---|---|
Most elegant way to check if the string is empty in Python? | stackoverflow |
How to check if text is “empty” (spaces, tabs, newlines) in Python? | stackoverflow |
isspace | python doc |
Python remove all whitespace in a string | stackoverflow |