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.
Use the in operator to check if a string contains a backslash, e.g. if '\\' in my_str: . Backslash characters have a special meaning in Python, so they have to be escaped with a second backslash. Copied! The backslash \ character has a special meaning in Python - it is used as an escape character (e.g. \n or \t ).
If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.
You need to escape the backslash.
s = "\\"
>>>mystring = "can python find this backslash \n"
>>>"\\" in r"%r" % mystring
True
while trying to filter the backslash \ character from being used in Windows filenames I searched a lot of places for answers. this is the solution worked for me. based on information I read at... http://pythonconquerstheuniverse.wordpress.com/2008/06/04/gotcha-%E2%80%94-backslashes-in-windows-filenames/
Backslashes are used for escaping, so to display a backslash in a string literal you need to escape the backslash with another backslash.
print "\\"
prints a string with 1 backslash.
"\\" in mystring
Where mystring is your string. Will tell you if it contains a backslash
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With