Find backslash (\) in a string --Python

限于喜欢 提交于 2019-12-12 11:53:18

问题


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.


回答1:


You need to escape the backslash.

s = "\\"



回答2:


>>>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/




回答3:


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.




回答4:


"\\" in mystring

Where mystring is your string. Will tell you if it contains a backslash



来源:https://stackoverflow.com/questions/11331778/find-backslash-in-a-string-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!