python print font with color

In the process of python development, we often encounter the need to print various information. A large amount of information is piled up in the console, which will cause the information to be mixed together, reducing the readability of important information. At this time, if you can add font colors to important information, it will be more convenient for users to read.

Of course, the display effect of the console is limited, and it can't be as cool as the previous paragraph, only some simple settings can be done. But from a readability point of view, it is much better.

The writing format and related instructions are as follows:

Copy code

Format:
  set color start: \033[display mode; foreground color; background color m

Description:
Foreground and background color
30 40 black
31 41 red
32 42 green
33 43 yellow
34 44 blue
35 45 fuchsia
36 46 cyan
37 47 white
Display mode meaning
0 terminal default setting
1 highlight
4 use underline
5 Flashing
7 Reversed display
8 Not visible

Example:
\033[1;31;40m
\033[0mCopy
code
Example 1:

print('\033[0;32;40m Welcome to use the student selection system\033[0m')
try:
num = int(input('Please enter the number selection function:'))
except Exception as e:
print('\033 [31m Sorry! The content you entered is wrong~\033[0m') The
result of the operation:

Example 2:

print('\033[0;36m firecrackers sound except for one year old,')
print('Spring breeze sends warmth into Tusu.')
print('Thousands of families and families on the day,')
print('Always change new peaches Old character. \033[0m')
Operation result:

Guess you like

Origin blog.csdn.net/shiguanggege/article/details/113791364