Python Day 2: Escape Characters

IntroductionWhen using the print statement to output variables, it can be inconvenient for certain special characters, such as newline and tab symbols, as shown in the code below.Python Day 2: Escape CharactersWhen printing this ancient poem, we often need to use four print statements. To output this content in a single line, we need to use the newline escape character.The meaning of escape is to use specific letter combinations to represent special characters.Returning to the problem we want to solve, to output a newline in the print statement, we need to use print(“\n”), where \n represents the newline character. For example, in the following program, although the phrase “春眠不觉晓处处闻啼鸟” is in a single print statement, the output result is still on two lines, which demonstrates the function of the \n escape character.Python Day 2: Escape Characters

All escape characters and their corresponding meanings:

Escape Character

Meaning

ASCII Code Value (Decimal)

\a

Bell (BEL)

007

\b

Backspace (BS), moves the cursor to the previous column

008

\f

Form feed (FF), moves the cursor to the beginning of the next page

012

\n

Newline (LF), moves the cursor to the beginning of the next line

010

\r

Carriage return (CR), moves the cursor to the beginning of the current line

013

\t

Horizontal tab (HT) (jumps to the next TAB position)

009

\v

Vertical tab (VT)

011

\\

Represents a backslash character “\\”

092

\’

Represents a single quote character

039

\”

Represents a double quote character

034

\?

Represents a question mark

063

\0

Null character (NUL)

000

\ddd

Any character represented by 1 to 3 digits in octal

Three-digit octal

\xhh

Any character represented in hexadecimal

Hexadecimal

Leave a Comment