[python] How do I compare a value to a backslash?

if (message.value[0] == "/" or message.value[0] == "\"):
    do stuff.

I'm sure it's a simple syntax error, but something is wrong with this if statement.

This question is related to python syntax

The answer is


If message.value[] is string:

if message.value[0] in ('/', '\'):
    do_stuff()

If it not str


Use following code to perform if-else conditioning in python: Here, I am checking the length of the string. If the length is less than 3 then do nothing, if more then 3 then I check the last 3 characters. If last 3 characters are "ing" then I add "ly" at the end otherwise I add "ing" at the end.

Code-

if (len(s)<=3):
    return s
elif s[-3:]=="ing":
    return s+"ly"
else: return s + "ing"

Try like this:

if message.value[0] == "/" or message.value[0] == "\\":
  do_stuff

When you only need to check for equality, you can also simply use the in operator to do a membership test in a sequence of accepted elements:

if message.value[0] in ('/', '\\'):
    do_stuff()