I would like to put multiple strings for one in statement one one line:
input = str(raw_input(''))
if str('a') or ('b') or ('c') in str(input):
print "a string"
else:
print "no"
Is it possible to have multiple strings for a single in statement?
First of all str('a') == 'a'
(because 'a'
is already a string), so you can leave that out.
'a' or 'b' or 'c' in input
This expression is parsed as the following:
('a') or ('b') or ('c' in input)
So you are checking if 'a'
or 'b'
evalute to true, which is the case. If you want to check them all using the in
operator, you have to explicitely specify that:
'a' in input or 'b' in input or 'c' in input
You can also simplify that then:
any(x in input for x in ('a', 'b', 'c'))
It is, but what you wrote does something entirely different. It checks to see if str('a')
is truthy, if 'b'
is truthy, or if 'c'
is in input
. While Python is very readable, it doesn't make assumptions about what you write. You have to be explicit:
if 'a' in input or 'b' in input or 'c' in input:
...
Or:
strings = ('a', 'b', 'c')
if any(s in input for s in strings):
...
Calling str()
with a string argument is pointless. Also, input
is the name of a builtin, so I suggest you rename your variable.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.