Python Tutorial – Escape Characters

Python Exercise Lists

The ASCII encoding assigns a unique number to each character, called an encoded value. In Python, an ASCII character can be represented by its encoded value, in addition to its entity (that is, the real character). This indirect use of encoded values to represent characters is called an escape character.

The ASCII encoding contains a total of 128 characters. \ 0 and \ x can only be followed by a maximum of two digits, this cause the octal form \ 0 cannot represent all ASCII characters. Only the hexadecimal form \ x can represent all ASCII characters.

We have been talking about ASCII encoding, and did not mention other encodings (character sets) such as Unicode, GBK, Big5, etc., because Python escape characters are only valid for ASCII encoding (128 characters).

Characters 1, 2, 3, x, y, and z correspond to the octal form of the ASCII code 61, 62, 63, 170, 171, 172, and the hexadecimal form are 31, 32, 33, 78, 79, 7A. The following example illustrates the use of escape characters:

str1 = "Oct: \061\062\063"
str2 = "Hex: \x31\x32\x33\x78\x79\x7A"

The output is:

Oct: 123
Hex: 123xyz

Note that escape characters in octal form cannot show xyz because their encoded values have three digits after being converted to octal.

For ASCII encoding, characters in the range of 0 ~ 31 (decimal) are characters which under control. They are invisible and cannot be displayed on the output devices or even entered from the keyboard. They can only be expressed in the form of escape characters. However, directly using ASCII code to memorize is not convenient, and it is not easy to understand. Therefore, for common control characters, C language also defines a shorthand method. The complete list of Python escape character is as follows.

Escape Characters Explanations
\n A newline character moves the cursor position to the beginning of the next line.
\r A carriage return moves the cursor to the beginning of the same line.
\t The horizontal tab, or Tab, is generally equivalent to four spaces.
\a The buzzer rings. Note that instead of sounding from the horn, many computers today do not have a buzzer, so the bell may not be effective.
\b Backspace, move the cursor position to the previous column.
\\ Backslash
\’ Single quote
\’’ Double quote
\ Continuation character at the end of the string line, that is, one line is not completed, go to the next line to continue writing.


#Use \t horizontal tab or tab
str1 = 'Hi, \t \t Welcome \t \t \t to \t \t my \t \t webpage'
str2 = 'Hi, \n Welcome \n to \n my \n webpage'
print (str1)
print (str2)


The output is:

Hi,  Welcome  to   my  webpage



More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression

More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises