[英]How to take "\" (backslash) as string and not as an "escape" character in python3?
In Python 3 I am trying to make a set containing a backslash as an element:在 Python 3 中,我试图创建一个包含反斜杠作为元素的集合:
a = {"/", "\"}
But I noticed that as soon as I closed the bracket after "\\"
, the bracket became blue.但我注意到,只要我在"\\"
之后关闭括号,括号就变成蓝色。 I, learned that \\
is an "escape" character used in things like \\r
, \\t
etc. But I want to take a backslash as a single piece of string.我了解到\\
是用于\\r
、 \\t
等内容的“转义”字符。但我想将反斜杠作为单个字符串。 How to prevent this problem?如何预防这个问题?
You need to escape the \\
by also using a backslash.您还需要使用反斜杠来转义\\
。 Therefore you will need two \\\\
因此,您将需要两个\\\\
Your string will then become a={"/","\\\\"}
你的字符串将变成a={"/","\\\\"}
In Python strings, the backslash "" is a special character, also called the "escape" character.在 Python 字符串中,反斜杠 "" 是一个特殊字符,也称为“转义”字符。 It is used in representing certain whitespace characters: "\\t" is a tab, "\\n" is a newline, and "\\r" is a carriage return.它用于表示某些空白字符:“\\t”是制表符,“\\n”是换行符,“\\r”是回车符。 you can use this for \\ and /:您可以将其用于 \\ 和 /:
>>> print('apple\torange')
apple orange
>>> print('apple\norange')
apple
orange
>>> print('\\')
\
>>> print('/')
/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.