Python terminal prints print with color

Source: Internet
Author: User

principle

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). writing format:
First part:\033[display mode; foreground color; background colour m + End part:\033[0m Explanation:
    • The first part of the three parameters: Display mode, foreground color, background color is an optional parameter, you can write only one of them;
    • 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 identified;
    • It is recommended that you write in the default format specification.
    • For the end part, can also be omitted, but in order to write the specification, it is suggested \033[*** beginning, \033[0m end.
numeric representation of the parameter meaning: display mode:0 (default \), 1 (highlighted), 22 (not bold), 4 (underscore), 24 (non-underlined), 5 (blinking), 25 (non-blinking), 7 (inverted), 27 (non-inverting)
Foreground color:30 (Black), 31(red), 32(green), 33(yellow), 34(blue), 35(Magenta), 36(cyan), 37 (white)
Background color:40(Black), 41(red), (Green), (yellow), (Blue), (Magenta),46 (cyan),(white) Sample Code Example code one (the function of the first parameter-changing the display mode):
Print("display mode:")  Print("\033[0;37;40m\thello world\033[0m")  Print("\033[1;37;40m\thello world\033[0m")  Print("\033[22;37;40m\thello world\033[0m")  Print("\033[4;37;40m\thello world\033[0m")  Print("\033[24;37;40m\thello world\033[0m")  Print("\033[5;37;40m\thello world\033[0m")  Print("\033[25;37;40m\thello world\033[0m")  Print("\033[7;37;40m\thello world\033[0m")  Print("\033[27;37;40m\thello world\033[0m")

The effect of example code one implementation:

Example code two (the role of the second parameter-changing the font color):

Print("Foreground color:")  Print("\033[0;30;40m\thello world\033[0m")  Print("\033[0;31;40m\thello world\033[0m")  Print("\033[0;32;40m\thello world\033[0m")  Print("\033[0;33;40m\thello world\033[0m")  Print("\033[0;34;40m\thello world\033[0m")  Print("\033[0;35;40m\thello world\033[0m")  Print("\033[0;36;40m\thello world\033[0m")  Print("\033[0;37;40m\thello world\033[0m")

The effect of the example code two implementation:

Example code three (the function of the third argument-changing the background color):

Print("Background color:")  Print("\033[0;37;40m\thello world\033[0m")Print("\033[0;37;41m\thello world\033[0m")Print("\033[0;37;42m\thello world\033[0m")Print("\033[0;37;43m\thello world\033[0m")Print("\033[0;37;44m\thello world\033[0m")Print("\033[0;37;45m\thello world\033[0m")Print("\033[0;37;46m\thello world\033[0m")Print("\033[0;37;47m\thello world\033[0m")

The effect of the three implementations of the sample code:

Python terminal prints print with color

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.