The Python Oracle

Returning the truthiness of a variable rather than its value?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping

--

Chapters
00:00 Returning The Truthiness Of A Variable Rather Than Its Value?
00:26 Accepted Answer Score 11
00:36 Thank you

--

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

--

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
>>>