Python 如何打印单个反斜杠?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19095796/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:51:09  来源:igfitidea点击:

How to print a single backslash?

pythonescapingbackslash

提问by Michael

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?

当我写print('\')print("\")或者print("'\'"),Python不打印反斜线\符号。相反,前两个错误并打印''第二个。我应该怎么做才能打印反斜杠?

采纳答案by Bucket

You need to escape your backslash by preceding it with, yes, another backslash:

您需要通过在它前面加上另一个反斜杠来转义反斜杠:

print("\")

And for versions prior to Python 3:

对于 Python 3 之前的版本:

print "\"

The \character is called an escape character, which interprets the character following it differently. For example, nby itself is simply a letter, but when you precede it with a backslash, it becomes \n, which is the newlinecharacter.

\字符称为转义字符,它以不同的方式解释其后的字符。例如,n它本身只是一个字母,但是当你在它前面加上一个反斜杠时,它就变成了\n,这就是newline字符。

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.

正如您可能猜到的那样,\也需要进行转义,因此它的功能不像转义字符。你必须……逃离逃离,本质上。

See the Python 3 documentation for string literals.

有关字符串文字,请参阅Python 3 文档

回答by lelloman

you should escape it...with \

你应该逃避它......用\

print('\')

回答by Andy

A backslash needs to be escaped with another backslash.

反斜杠需要用另一个反斜杠转义。

print('\')

回答by Paulo Bu

If you're trying to accomplish something more complicated than just printing a backlash, you can declare a string as a raw string by adding an rin front of it and it will print all its characters as is:

如果你想完成比打印反冲更复杂的事情,你可以通过r在字符串前面添加一个来将字符串声明为原始字符串,它会按原样打印所有字符:

>>> s = r'\abc\def'
>>> print(s)
'\abc\def'

This is useful for regular expressions.

这对正则表达式很有用。

Note that a raw string can't endwith a backslash. You can find more information in the docs.

请注意,原始字符串不能以反斜杠结尾。您可以在文档中找到更多信息

回答by Sanyal

Uses of escape character (command followed by output):

转义字符的使用(命令后跟输出):

>>> print "\"
\
>>> print 'he\'s pythonic'
he's pythonic

The backslash "\" helps python to understand that in 'he\'s pythonic', ['he's] not eol

反斜杠“\”帮助 python 理解在'he\'s pythonic', ['he's] not eol

回答by Harsha Biyani

Try :

尝试 :

>>> s = r'Tea\coffee'
>>> s
'Tea\coffee'
>>> print(s)
Tea\coffee
>>> print("\")
\
>>> len("\")
1

回答by Noah Broyles

In Python, a backslash (\) is an escape character. So if you want to print a single backslash, use:

在 Python 中,反斜杠 ( \) 是转义字符。因此,如果要打印单个反斜杠,请使用:

print("\") # the first backslash escapes the second one

Also, the backslash can be used to escape the same quotation marks you use to delimit a string. Like this:

此外,反斜杠可用于转义用于分隔字符串的相同引号。像这样:

print('What\'s up?')

Without the \, Python would think the string ends at the 'in What's. The \allows you to escape the 'mark and print it instead of closing the string with it.

如果没有\,Python 会认为字符串以'in结尾What's。将\让你逃跑的'标记,并打印它,而不是与它关闭的字符串。