The Python Oracle

Returning the truthiness of a variable rather than its value?

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC F Haydns String Quartet No 53 in D

--

Chapters
00:00 Question
00:33 Accepted answer (Score 11)
00:48 Thank you

--

Full question
https://stackoverflow.com/questions/2728...

Accepted answer links:
[bool]: https://docs.python.org/3/library/functi...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #string #boolean #truthiness

#avk47



ACCEPTED ANSWER

Score 11


You can use bool:

return bool(test_string)

Demo:

>>> bool('abc')
True
>>> bool('')
False
>>>