Reputation: 6358
Total newbie python question. I find myself having to write to the following code
s: str
if s is None or len(s) <= 0
to check for string validation.
I finally wrote a tiny function to do that for me.
Is there a more pythantic way of doing this?
Upvotes: 1
Views: 8888
Reputation: 2432
You can use not s
:
>>> not None
True
>>> not ''
True
>>> not 'foo'
False
Upvotes: 2
Reputation: 3113
If your real goal is to find out if s
is a string, you can use isinstance()
.
isinstance(s,str)
Upvotes: 0
Reputation: 368
The string will be "truthy" if it is not None or has len > 0
. Simply do the following check:
if s:
print("The string is valid!")
Upvotes: 5