How do I compare a value to a backslash?

Viewed 171706
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.

5 Answers

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()

Escape the backslash:

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

From the documentation:

The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character.

Try like this:

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