An implementation process
The character color of terminal is controlled by escape sequence, and it is the system display function in text mode, which is independent of the specific language.
The escape sequence begins with the ESC, which is done with \033 (the ASCII code for ESC is 27 in decimal notation, or 033 in octal).
Two writing formats
opening Section :\033[ display mode; foreground color; background colour m
End part: \033[0m
Full format: \033[ display mode; background color m text to be printed \033[0m If there are spaces, the spaces will be printed .
Note: The first part of the three parameters: Display mode, foreground color, background color is an optional parameter, you can write only one of them, in addition, because the three parameters of the different meaning of the values are unique and no duplication, so the three parameters of the writing sequence is not fixed requirements, the system can be recognized; It is recommended that you write in the default format specification.
For the end part, can also be omitted, but omitted, if the background color is printed, then the entire row will have a background color (including the parts without fonts), so in order to write the specification, it is recommended \033[*** beginning, \033[0m end.
the three values indicate the meaning of the parameter:
display mode:0 (default), 1 (highlighted, bold), 4 (underscore), 7 (inverted),
Foreground color:30 (Black), 31(red), 32(green), 33(yellow), 34(blue), 35(plum color), 36(cyan), 37 (white)
Background color:40(Black), a (red), a(green), a(yellow), A (blue), 45 (plum), (cyan), 47 (white)
Four Common opening formats :
\033[0m default font normal display, not highlighted
\033[32;0m red font normal display
\033[1;32;40m display: Highlight font foreground color: Green background color: Black
\033[0;31;46m display mode: Normal font foreground color: Red background color: Cyan
Five examples:
(1)
Print("\033[1;31;40m] The account or password you entered is wrong! \033[0m] standard notation
Print("\033[1;31;40m] The account or password you entered is wrong! ")
The output format of the above code is: font highlighting, red foreground, yellow background PS: foreground color is the font colour
The results of the above two lines of code are as follows:
For the end part, you can actually omit it, but if you print the background color, the entire row will have a background color (including parts without fonts).
(2)
Print ("\033[0;31m%s\033[0m"% "output red character") standard notation
Print ("\033[31m%s\033[0m"% "output red character") is displayed as 0 o'clock and can be omitted
Print ("\033[31m%s"% "output red character")
#上方代码的输出格式为: font default, red foreground, no background color
The results are as follows:
No background color, the above 3 ways to get the desired effect
Python terminal output Print color font method