Escape sequence

Escape sequence

In addition to the information that can be recognized as "characters" by humans, the characters used in the OS also include information other than "characters" such as control characters and character editing information on the OS as character information. These characters are sometimes called "special characters". This special character has an escape sequence for humans to input and convey to the OS. There is no problem if you use the escape sequence intentionally and display special characters, but without any consideration, put the \ mark in the program or in the text file that imports the data to the DB. If it is included, the OS interprets the \ mark as part of the escape sequence, and an error may occur or data may be registered / displayed in an unintended state.

In Python, you can define it as a string containing line breaks by adding three quotation marks ("" "or"'"). However, when defining a character string including line breaks as a variable in an indented block, etc. The indentation will shift and it will be difficult to see. [Example] def func(): words = “” ”Do not cross the flow of the river Moreover, it is not in the original water, "" "" print(words) func() ↓ Do not cross the flow of the river Moreover, it is not in the original water

In the above case, if you put an indent at the beginning of the character after the line break, that part will also be interpreted as a part of the character string. In such a case, you can use the escape sequence "\ n" to describe the character string including line breaks on one line, and prevent the indentation from collapsing. [Example] def func(): words = "Do not cross the flow of the river that goes on. print(words) func() ↓ Do not cross the flow of the river Moreover, it is not in the original water

Escape sequences are for embedding control codes such as line breaks and tabs. If you want to put double quotes in a string enclosed in double quotes, or if you want to put ASCII or Unicode characters It can also be used when embedding as a numerical value.

[Frequently used escape sequences]

Escape sequence Description
\ N Line breaks
\ R Line feed (CR, carriage return)
\ T Horizontal tab
¥ f Page break (form feed)
¥ ’ Single quotes
¥ ” Double quotes
\ Backslash
\ X61 8-bit characters corresponding to hexadecimal numbers
\ U3042 Unicode characters corresponding to 16-bit hexadecimal, "0x" is not required for the hexadecimal part
¥ 0 null character

In a triple quote string, unless you terminate the string with a triple unescaped quote character, You can write unescaped newlines and quotes (and they remain in the string). (The "quote" here refers to the character used to start enclosing the string, either'or'). Unless prefixed with "r" or "R", escape sequences in strings are interpreted according to the same rules used in standard C. Below is the escape sequence recognized by Python.

Recommended Posts

Escape sequence
Escape __init__.py
Sequence and mapping
String to Unicode Escape Sequence Format for Python