What's the logical value of "string" in Python?
Solution 1
Any non empty string in Python (and most other languages) is true as are all non-zero numbers and non-empty lists, dictionaries, sets and tuples.1
A nicer way to do what you want is:
name = input("what is your name?")
if name in ("Kamran", "Samaneh"):
print("That is a nice name")
else:
print("You have a boring name ;)")
This creates a tuple containing the names that you want and performs a membership test.
1 As delnan points out in the comments, this applies to all well written collections. That is, if you implement a custom collection class, make sure that it is false when it's empty.
Solution 2
Besides the empty string ''
, strings will all evaluate to True
(see this page for a full list of values of all types that evaluate to False
. This follows the logic of many other programming languages (except some which also evaluate strings like '0'
, 'false'
, etc. to False
). The exact decision of what to do is somewhat arbitrary, but the choice made can be explained as allowing the cast to be used as a simple way to test for empty (default, or unpopulated) strings.
You can always force a cast of any type to bool
using the bool()
function.
>>> bool('')
False
>>> bool('non-empty string')
True
>>> bool('0')
True
>>> bool('False')
True
>>> bool('false')
True
Solution 3
http://docs.python.org/library/stdtypes.html#truth-value-testing
"....All other values are considered true — so objects of many types are always true."
Solution 4
In Python an empty string is considered False
, True
otherwise.
You could use the in
operator:
if name in ("Kamran","Samaneh"):
print("That is a nice name")
else:
print("You have a boring name ;)")
Related videos on Youtube
Kamran Bigdely
My full name is "Kamran Bigdely-Shamloo". My nickname is "Kami". I have (almost) lost my hope in humanity and waiting for the singularity to happen. Any breakthrough in science and technology (especially in Cancer research) gives me an intellectual orgasm. My interests: Writing tutorials, Teaching to smart students. Game Developments Cooking, Indoor Gardening So many other things that life does not have time for...
Updated on June 04, 2022Comments
-
Kamran Bigdely almost 2 years
I erroneously wrote this code in Python:
name = input("what is your name?") if name == "Kamran" or "Samaneh": print("That is a nice name") else: print("You have a boring name ;)")
It always prints out "That is a nice name" even when the input is neither "Kamran" nor "Samaneh".
Am I correct in saying that it considers "Samaneh" as a true? Why?
By the way, I already noticed my mistake. The correct form is:
if name == "Kamran" or name == "Samaneh":
-
Admin over 13 yearsNot only lists, dicts and tuples -- all empty collections (if well-written).
-
aaronasterling over 13 years@delnan, I was trying to think of a good way to say that so I stuck to the built ins and forgot sets.
-
Kabie over 13 yearsThere is no
raw_input
in Python3 andif name == ("Kamran" or "Samaneh")
is just wrong asif (name == "Kamran" or "Samaneh")
. -
Kamran Bigdely over 13 yearsI tested name == ("Kamran" or "Samaneh"). When I enter "Samaneh" it gives me "You have a boring name ;)" which is not correct.
-
eyquem over 13 yearsI absolutely understand nothing why I posted such a false and stupid answer. Despite the fact I had studied this matter of chains considered as booleans in Python some time ago, and I thought I had well understood the thing, I completely screwed up.
-
eyquem over 13 yearsUsually, I always test a script before I post. Here, it seems I didn’t, since the expression name == ("Kamran" or "Samaneh") gives True only when name is ’Kamran’ and therefore can’t be used to detect when name has the value ’Samaneh“.
-
eyquem over 13 yearsThe fact is that ("Kamran" or "Samaneh") is always “Kamran“ and that’s not particularly difficult to see. I hope Kamran edited his question only after I read it, because now it is well pointed out in his question that the correct form is if name == "Kamran" or name == "Samaneh": and if this would have been already in the question when I read it, it would be even more stupid for me not to have been alerted.
-
eyquem over 13 yearsI am not pleased to have received downvotes but they are justified. I don’t understand what I got up to with this problem, I think it was late and I was tired.
-
eyquem over 13 yearsPS I had problems to post this comment in only one chunk. I don’t know if it’s because of my out-to-date operating system that displays pages weirdly, or if the normal working of stackoverflow. OS or SO ? :)
-
Daniel H almost 7 yearsMost objects, collection or otherwise, are truthy; only the ones wheich are explicitly define otherwise aren’t. If you just define a class with
class C(): pass
, instances will test asTrue
.