Python Programming – Python Character Set

You can learn about Introduction to Python Programming Programs with Outputs helped you to understand the language better.

Python Programming – Python Character Set

The character set is a set of valid characters that a language can recognize. Python uses the character set to declare identifiers. There are two types of character sets in the Python language. These are:

(a) Source characters
(b) Execution characters /Escape sequences

Source Characters

Using source characters, the source text is created. Following is the list of source characters:
Alphabets : A to Z, a to z and _ (underscore)
Numbers : 0 to 9
Special characters : + – * / A ~% = ! & I (){}[]?”, ;: \ # blank ”

Execution Characters / Escape Sequences

These characters are interpreted at the time of execution. The values of execution characters are implementation-defined.
The characters on the keyboard can be printed or displayed by pressing the key. But some characters, such as line feed, form feed, and tab, cannot be printed or displayed directly. Python provides the mechanism to get such characters that are invisible or difficult to represent directly through execution characters. In Python \n, \r, \t, \f and \v are considered as whitespace characters. Execution charac¬ters and escape sequences are used interchangeably.

Execution Character Meaning Result at Execution Time
\n End of a line Moves the active position to the initial position of the next line. (See Figure 3.21).
\r Carriage return Moves the active position to the initial position of the next paragraph.
\f Form feed Moves the active position to the initial position of the next logical page.
\v Vertical tab Moves the active position to the next vertical tabulation position.
\t Horizontal tab Moves the active position to the next horizontal tabulation position. (See Figure 3.21).
\b Backspace Moves the active position to the previous posi­tion on the current line.
\a Alert Produces an audible alert.


Example 1 – Demo of backslash (\)

# Demo of backslash (\)
print (‘A\nb\nc’)
print (‘a\tb\tc’)

a b c

Escape codes are embedded inside string literals and start with a backslash character (\). They are used to embed characters that are either unprintable or have a special syntactic meaning to Python that you want to suppress. Each of the escape sequences shall produce a unique implementation-defined value that can be stored in a single character. These escape sequences are represented by a backslash (\) followed by a character. Though there are two or more characters in an escape sequence, they are considered as a single character. Some of them are shown in Table 3.2.

Many other characters, after the backslash (\), in output statements will result in the display of the character itself. For example, the execution of \ k will result in displaying of k.

Recommended Reading On: Java Program to Print Alphabet Y Star Pattern

Leave a Reply

Your email address will not be published. Required fields are marked *