Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does `and` return a string instead of `True` or `False`?

Quick answer

In Python, and uses truthiness to choose an operand; it does not promise to return True or False. It returns the first operand if that operand is false in a Boolean context; otherwise, it evaluates and returns the second operand. Since an empty string is false and a nonempty string is true, string results are expected. Use bool(expression) or an explicit comparison when you need a Boolean result. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

Why does this print a string instead of True?

name = "Mina"
result = name and "Welcome"
print(result)

I thought and always returned a Boolean.

Geez

The key distinction is that Python uses truthiness to decide what and returns, but the result is one of its operands—not necessarily a Boolean. Python evaluates x and y by returning x when x is false in a Boolean context; otherwise, it evaluates and returns y. [S1]

Here, name is the nonempty string "Mina", so it is true in a Boolean context. Python therefore evaluates the second operand and returns "Welcome". That string is the result, rather than the Boolean True.

This can be useful when you want a value only if an earlier value is present. But if your goal is specifically to get True or False, you need to ask for that explicitly.

Follow-up question

Can you show how the same expression behaves when the string is empty, and give me a small runnable example?

Geez

An empty string is false in a Boolean context, while a nonempty string is true. So with and, an empty first string is returned as-is; with a nonempty first string, the second operand is returned. [S1]

empty_name = ""
name = "Mina"

print(empty_name and "Welcome")  # prints an empty string
print(name and "Welcome")        # prints: Welcome

The first printed line may look blank because its value is "". The second line displays Welcome. These outcomes follow the operand-selection rule: Python returns the first operand if it is false; otherwise, it returns the second. [S1]

The blank output is a useful reminder that the result can be an empty string, not the Boolean False.

Follow-up question

What should I write if I need a real Boolean, for example to check whether both strings contain something?

Geez

If you need a Boolean result, use bool(...) around the expression, or write an explicit comparison that states what you want to test. The Python reference recommends bool(expression) or an explicit comparison when a Boolean result is needed. [S1]

For example:

first = "Mina"
second = "Welcome"

both_nonempty = bool(first and second)
print(both_nonempty)  # True

Because both strings are nonempty, the and expression returns the second string, and bool(...) converts that result into a Boolean. [S1]

You can also make the checks explicit:

both_nonempty = first != "" and second != ""

This expresses the condition directly: each string must differ from the empty string. Choose the version that makes your intent clearer to someone reading the code. The examples assume the values are strings; if your variables may contain other types, decide what “contains something” should mean for those values rather than assuming the same check fits.

Follow-up question

How can I quickly tell what result an and expression will produce when I’m debugging?

Geez

Read the expression from left to right and ask two questions: “Is the first operand false in a Boolean context?” and “If not, what is the second operand?” If the first is false, that exact first value is the result; otherwise, the second operand is evaluated and returned. [S1]

For example, in "" and "ready", the empty string is false, so the result is "". In "ready" and "done", the first string is true, so the result is "done". [S1]

A useful rule of thumb: use and to select a value based on truthiness; use bool(...) or explicit comparisons when the result itself must be Boolean.

A practical checklist

  • Identify the first operand and check whether it is false in a Boolean context.

  • If the first operand is false, expect and to return that operand; otherwise expect it to return the second operand.

  • Remember that an empty string is false and a nonempty string is true in a Boolean context.

  • Wrap the expression in bool(...) or use explicit comparisons when you require True or False.

  • Confirm that truthiness matches the condition you intend to test.

A few more useful answers

Does `and` always evaluate its second operand?

No. Python evaluates the second operand only when the first operand is true in a Boolean context; otherwise, it returns the first operand. [S1]

Why does `"hi" and "there"` return `"there"`?

A nonempty string is true in a Boolean context, so Python evaluates and returns the second operand. [S1]

How do I make the result of an `and` expression show as `True` or `False`?

Use bool(expression) or write explicit comparisons for the conditions you mean to test. [S1]

Sources

References used for this guide.

  1. [S1] Python Language Reference: Boolean operations and operand selection
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work