Find backslash (\) in a string --Python

Ignacio Gómez picture Ignacio Gómez · Jul 4, 2012 · Viewed 33.5k times · Source

How can I compare if a backslash is in my string?

I don't know how to write the backslash symbol to compare it. I try this but don't work:

Code:

s = r"\""
print s

Output: \"

If I try s = "\"" it gives " as output

I don't know how to acheive that.

Thanks for any help.

Answer

Ignacio Vazquez-Abrams picture Ignacio Vazquez-Abrams · Jul 4, 2012

You need to escape the backslash.

s = "\\"