为什么打印“\\"只打印出一个反斜杠? [英] Why does print "\\" only print out a single backslash?

查看:51
本文介绍了为什么打印“\\"只打印出一个反斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下 Python 2.7 代码:

Consider the following Python 2.7 code:

 print "\\"

预期结果:\\

实际结果:\

为什么 Python 只打印出一个反斜杠?

Why does Python only print out a single backslash?

推荐答案

这是因为 \ 是转义符,它转义了换行符和回车符等序列.要打印出两个,您可以执行以下操作:

It's because \ is the escape character, it escape sequences like newlines and carriage returns. To print out two you can do:

print "\\\\"

或者:

print r"\\"

r 前缀告诉忽略转义字符.

r prefix tells to ignore escape characters.

这篇关于为什么打印“\\"只打印出一个反斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆