Python 简明教程
Python - Escape Characters
Escape Character
escape character 是一个字符后跟一个反斜杠 (\)。它告诉 Interpreter 这个转义字符(序列)有特殊含义。例如,\n 是一个转义序列,表示换行符。当 Python 在字符串中遇到这个序列时,它会理解需要开始一个新行。
An escape character is a character followed by a backslash (\). It tells the Interpreter that this escape character (sequence) has a special meaning. For instance, \n is an escape sequence that represents a newline. When Python encounters this sequence in a string, it understands that it needs to start a new line.
除非存在 'r' 或 'R' 前缀,否则字符串和字节字面量中的转义序列的解释规则与标准 C 中使用的规则类似。在 Python 中,如果一个 string 在引号符号之前以 "r" 或 "R" 为前缀,它将成为一个原始字符串。因此,'Hello' 是一个正常字符串,而 r’Hello' 是一个原始字符串。
Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. In Python, a string becomes a raw string if it is prefixed with "r" or "R" before the quotation symbols. Hence 'Hello' is a normal string whereas r’Hello' is a raw string.
Example
在下面的示例中,我们实际展示了原始字符串和普通字符串。
In the below example, we are practically demonstrating raw and normal string.
# normal string
normal = "Hello"
print (normal)
# raw string
raw = r"Hello"
print (raw)
以下代码的输出如下:
Output of the above code is shown below −
Hello
Hello
在正常情况下,两者之间没有区别。但是,当转义字符嵌入在字符串中时,普通字符串实际上会解释转义序列,而原始字符串不会处理转义字符。
In normal circumstances, there is no difference between the two. However, when the escape character is embedded in the string, the normal string actually interprets the escape sequence, whereas the raw string doesn’t process the escape character.
Example
在以下示例中,当打印一个普通字符串时,转义字符 '\n' 被处理为引入一个换行符。但是,由于原始字符串运算符 'r',转义字符的效果不会按照其含义翻译。
In the following example, when a normal string is printed the escape character '\n' is processed to introduce a newline. However, because of the raw string operator 'r' the effect of escape character is not translated as per its meaning.
normal = "Hello\nWorld"
print (normal)
raw = r"Hello\nWorld"
print (raw)
运行以上代码,它将打印以下结果:
On running the above code, it will print the following result −
Hello
World
Hello\nWorld
Escape Characters in Python
下表显示了 Python 中使用的不同转义字符:
The following table shows the different escape characters used in Python -
Escape Characters Example
以下代码显示了上表中列出的转义序列的用法:
The following code shows the usage of escape sequences listed in the above table −
# ignore \
s = 'This string will not include \
backslashes or newline characters.'
print (s)
# escape backslash
s=s = 'The \\character is called backslash'
print (s)
# escape single quote
s='Hello \'Python\''
print (s)
# escape double quote
s="Hello \"Python\""
print (s)
# escape \b to generate ASCII backspace
s='Hel\blo'
print (s)
# ASCII Bell character
s='Hello\a'
print (s)
# newline
s='Hello\nPython'
print (s)
# Horizontal tab
s='Hello\tPython'
print (s)
# form feed
s= "hello\fworld"
print (s)
# Octal notation
s="\101"
print(s)
# Hexadecimal notation
s="\x41"
print (s)
它将生成以下 output −
It will produce the following output −
This string will not include backslashes or newline characters.
The \character is called backslash
Hello 'Python'
Hello "Python"
Helo
Hello
Hello
Python
Hello Python
hello
world
A
A