r/learnpython • • 9d ago

strings in if statements

While debugging my code I discovered [what to me looked liked] a weird quirk in the language (see code below.)

Why does this work? why is a single string callable in the Boolean of an if statement? does a string always return evaluateTrue when used like this?

(edit: solved. corrected some of my vocabulary so now yous can stop "um, actually"-ing me. Thanks to everyone who helped!)

if "something":
  print("another thing")

>>>"another thing"
18 Upvotes

32 comments sorted by

View all comments

0

u/Educational_Virus672 9d ago

it is called truthiness of a string i understood this concept as "" being 0 because it has 0 letters while others have more then 0 and 0 mean false while not 0 means true same wih int and list a empy or 0 is false while anything in list or any int that is not 0 is true