Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compare a value to a backslash?

Tags:

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.

like image 944
DizzyDoo Avatar asked Jan 13 '10 18:01

DizzyDoo


People also ask

How do I find a backslash in a string?

Backslashes are used for escaping, so to display a backslash in a string literal you need to escape the backslash with another backslash.

What is a backslash in pandas?

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

What does '\' do in Python?

In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.


1 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() 
like image 94
poke Avatar answered Oct 21 '22 22:10

poke