问题描述
当我写 print('\')或 print( \)或 print('\'),Python不会打印反斜杠 \ 符号。相反,它会为前两个错误,并在第二个显示’。我应该怎么做才能打印反斜杠?
When I write print('\') or print("\") or print("'\'"), Python doesn't print the backslash \ symbol. Instead it errors for the first two and prints '' for the second. What should I do to print a backslash?
推荐答案
您需要在反斜杠前面加上另一个反斜杠来转义:
You need to escape your backslash by preceding it with, yes, another backslash:
print("\\")
并且对于Python 3之前的版本:
And for versions prior to Python 3:
print "\\"
\ 字符称为转义字符,对后面的字符有不同的解释。例如, n 本身只是一个字母,但是当您在其前面加上反斜杠时,它变为 \n ,这是换行符字符。
The \ character is called an escape character, which interprets the character following it differently. For example, n by itself is simply a letter, but when you precede it with a backslash, it becomes \n, which is the newline character.
您可能会猜到, \ 也需要转义,因此它的功能不像转义符。
As you can probably guess, \ also needs to be escaped so it doesn't function like an escape character. You have to... escape the escape, essentially.
请参阅。
这篇关于如何打印单个反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!