U
U
Untiwe2021-12-02 16:09:09
Python
Untiwe, 2021-12-02 16:09:09

Why is string not equal to true?

A non-empty string in python is True , but comparing it to a boolean value yields False :

print("Hello world" == True)
# False

Here the string is implicitly cast to the bool type and becomes True , it turns out True == True , but for some reason False in the answer .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2021-12-02
@Untiwe

Non empty string in python is True

Nope. A non- empty string is true in boolean context , but that doesn't mean it's True.

S
soremix, 2021-12-02
@SoreMix

Here the string is not explicitly cast to the type bool

Not given. A string is a string. Otherwise, the condition 'a' == 'b'would also be true, and both are True.
Probably confused with if 'Hello world':, in this case something like ->if bool('Hello world'):if True:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question