r/learnpython • • 8d 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/[deleted] 8d ago

[deleted] — view deleted comment

0

u/AlexMTBDude 8d ago

Strings don't return anything, only functions return something. A non-empty string evaluates to True while an empty string evaluates to False.

0

u/Chorizo_n_eggs 8d ago

When strictly referring to Python, the use of return and evaluate are very different. Hardly being pedantic imo.

0

u/Alternative-Emu2000 8d ago

That doesn't justify the abusive DMs and referrals to suicide helplines by the pathetic twat who corrected me.