简体   繁体   English

我怎么知道要在Python中添加多少个反斜杠

[英]how do I know how many backslash to add in Python

Why I need to add 5 backslash on the left if I want to show three of them in Python? 如果要在Python中显示三个反斜杠,为什么还要在左侧添加5个反斜杠? How to count the backslash? 如何计算反斜杠?

# [ ] print "\\\WARNING!///"
print('"\\\\\Warning///"')

You can use a "raw string" by adding r : 您可以通过添加r来使用“原始字符串”:

print(r'"\\\Warning///"')

This helps to avoid the backslash's "escape" properties, which python uses to control the use of special characters 这有助于避免反斜杠的“转义”属性,python使用该属性来控制特殊字符的使用

大多数情况下,反斜杠被用作转义序列,因此要打印单个\\则需要使用\\\\

according to https://docs.python.org/2.0/ref/strings.html 根据https://docs.python.org/2.0/ref/strings.html

Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, ie, the backslash is left in the string. 与标准C不同,所有无法识别的转义序列都保留在字符串中不变,即,反斜杠保留在字符串中。

since \\W is not a valid escape sequence, \\W is printed as it is. 由于\\W不是有效的转义序列,因此按原样打印\\W on the other hand \\\\ is printed as \\ . 另一方面\\\\被打印为\\

so \\\\\\\\\\W is printed as \\\\\\W 因此\\\\\\\\\\W打印为\\\\\\W

However, in python 3.6, according to Strings and bytes literals 但是,在python 3.6中,根据字符串和字节文字

Changed in version 3.6: Unrecognized escape sequences produce a DeprecationWarning. 在版本3.6中更改:无法识别的转义序列会产生DeprecationWarning。 In some future version of Python they will be a SyntaxError. 在将来的Python版本中,它们将是SyntaxError。

So your code might give SyntaxError in future python. 因此,您的代码可能在将来的python中提供SyntaxError。

Unlike standard C, any unrecognized escape sequences are left unchanged in python. 与标准C不同,任何无法识别的转义序列在python中均保持不变。

>>> print('\test')
'    est'  # '\t' evaluates to tab + 'est'

>>> print('\\test')
'\test'  # '\\' evaluates to literal '\' + 'test'

>>> print('\\\test')
'\    est'  # '\\' evaluates to literal '\' + '\t' evaluates to tab + 'est'

>>> print('\\\\test')
'\\test'  # '\\' evaluates to literal '\' + '\\' evaluates to literal '\' + 'test'

Actually you should type 2n backslashes to represent n of them, technically. 实际上,从技术上讲,您应该键入2n反斜杠来表示其中的n个。 In a strict grammar, backslash is reserved as an escape character and has a special meaning, ie, it does not represent "backslash". 在严格的语法中,反斜杠保留为转义符,并具有特殊含义,即它不表示“反斜杠”。 So, we took it from our character set to give it a special meaning then how to represent a pure "backslash"? 因此,我们从字符集中获取了它的特殊含义,然后又如何表示一个纯的“反斜杠”? The answer is represent it in the way newline character is represented, namely '\\' stands for a backslash. 答案是用换行符表示的,即“ \\”代表反斜杠。

And the reason why you get 3 backslashes printed when 5 is typed is: The first 4 of them is interpreted as I said above and when it comes to the fifth one, the interpreter found that there is no definition for '\\W' so it treated the fifth backslash as a normal one instead of a escape character. 键入5时得到3个反斜杠的原因是:如前所述,我解释了前四个,而对于第五个,解释器发现“ \\ W”没有定义,因此将第五个反斜杠视为普通的反斜杠,而不是转义字符。 This is an advanced convenience feature of the interpreter and might not be true in other versions of it or in other languages (especially in more strict ones). 这是解释器的高级便利功能,在其他版本的解释器或其他语言(尤其是较严格的语言)中可能不正确。

\\ is used for special characters like '\\n' , '\\t' etc. You should type 2n or 2n-1 \\ for printing n \\ . \\用于特殊字符,例如'\\n''\\t'等。您应该键入2n2n-1 \\来打印n \\

>>> print('\warning') # 1 \
\warning
>>> print('\\warning') # 2 \
\warning
>>> print('\\\warning') # 3 \
\\warning
>>> print('\\\\warning') # 4 \
\\warning
>>> print('\\\\\warning') # 5 \
\\\warning
>>> print('\\\\\\warning') # 6 \
\\\warning
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM