Python Learning-terminal font highlighting 1

Source: Internet
Author: User

1, using the native escape character sequence, the version of Windows does not support (such as Win7), the perfect support for Linux

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).

Format:

opening Section :\033[display mode, foreground color, background colour m + End part:\033[0m

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 identify However, 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. With this native escape sequence output, fully supported under Linux, but there is a compatibility problem under Windows, such as the normal display of color under WIN10, under Win7 does not support. Therefore, you can use the Python standard library to provide the Colorama module output color font, this module is cross-platform, the internal implementation is also the escape sequence to display color, but the Windows platform to do special processing, so fully compatible with Linux and Windows versions. the meaning of the numeric parameter representation: Description:
Front view Background color Color
30 40 Black
31 41 Red
32 42 Green
33 43 Yellow
34 44 Blue
35 45 Purplish red
36 46 Cyan Blue
37 47 White

Display mode:

Display mode Significance
0 Terminal default settings
1 Highlight Display
4 Use underline
5 Flashing
7 Anti-white display
8 Not visible
 1 #!/usr/bin/env python3 2 #-*-coding:utf-8-*-3 # @Time: 2018/4/29 10:27 4 # @Author: Yang 5 # @File: Colo red_escape_character.py 6 # @Software: Pycharm 7 #--------------------------------8 #显示格式: \033[display mode; foreground color M 9 #------- -------------------------#显示方式 Description 11 # 0 Terminal default setting 12 # 1 Highlight 13 # 4 Use Dash 14 # 5 Blinking 15 # 7 Anti-white display 16 # 8 Invisible 17 # 22 Non-Bold 18 # 24 non-row                Line 19 # 25 non-blinking #21 #前景色 background Color 22 # 30 40 Black 23 # 31                41 Red 24 # 32 42 Green 25 # 33 43 Yellow 26 # 34                44 Blue 27 # 35 45 Magenta 28 # 36 46 Cyan Blue 29 # 37      47 White #---------------------------------------class Colored (object): RED = ' \033[31m ' #红色33   GREEN = ' \033[32m '  #绿色34 YELLOW = ' \033[33m ' #黄色35 BLUE = ' \033[34m ' #蓝色36 fuchsia = ' \033[35m ' #紫红色37 CYAN = ' 33[36m ' #青蓝色38 white = ' \033[37m ' #白色39 #:no color40 RESET = ' \033[0m ' #终端默认颜色41 def color_s TR (self,color,s): Return ' {}{}{} '. Format (GetAttr (Self,color), s,self. RESET) (self,s): Return self.color_str (' Red ', s), Def Green (self,s): Return SELF.C         Olor_str (' GREEN ', s)-def Yellow (self,s): Return self.color_str (' Yellow ', s)-def Blue (self,s): 51 Return self.color_str (' BLUE ', s) Fuchsia (self,s): self.color_str (' Fuchsia ', s)--def Cyan (s Elf,s): Self.color_str (' CYAN ', s) + def White (self,s): self.color_str return (' White ', s) 58 #- ----------use examples such as the next--------the color = Colored () print (color.red (' I am red! ')) Print (Color.green (' I am green! ')) Print (Color.yellow (' I am yellow! ')) Print (Color.Blue (' I am blue! ')) The PRINT (Color.fuchsia (' I am fuchsia! ')) Print (Color.cyan (' I am cyan! ')) Print (Color.White (' I am white! '))

Output Result:

2. Use the Python standard library Colorama module-compatible with Linux and Windows editions:

 1 #!/usr/bin/env python3 2 #-*-coding:utf-8-*-3 # @Time: 2018/4/29 10:57 4 # @Author: Yang 5 # @File: Colo red_colorama_module.py 6 # @Software: Pycharm 7 #--------------Some constants of the Colorama module-------8 #colorama是一个python专门用来在控制台, command line output color text module, can be used across the Platform 9 # under the Windows Linux work well, if you want to make the console output information more beautiful, you can use to this module. Fore:black, red, green, YELLOW, Blue, MAGENTA, CYAN, White, reset.11 # Back:black, Red, GREEN, YELLOW, Blue, magent A, CYAN, White, reset.12 # Style:dim, NORMAL, BRIGHT, reset_all13 from Colorama import init,fore,back,style14 #init (autor Eset=true) class colored (object): Def Red (self,s): return fore.red + S + fore.reset18 def green (self , s): return Fore.green + S + fore.reset20 def Yellow (self,s): return fore.yellow + S + Fore.reset2 2 def Blue (self,s): return fore.blue + S + fore.reset24 def Magenta (self,s): Return Fore.magen TA + S + fore.reset26 def Cyan (self,s): return Fore.cyan + S + FOre. RESET28 def White (self,s): return fore.white + S + fore.reset30 def balck (self,s): Return Fore . BLACK32 def white_green (self,s): return fore.white + back.green + S + fore.reset + back.reset34 color = Colo Red () Print (color.red (' I am red! ')) Print (Color.green (' I am green! ')) PNS Print (Color.yellow (' I am yellow! ')) Print (Color.Blue (' I am blue! ')) Print (Color.magenta (' I am magenta! ')) Print (Color.cyan (' I am cyan! ')) Print (Color.White (' I am white! ')) Print (Color.white_green (' I am White green! '))
Output Result: 3 , using Python's Termcolor module:

Termcolor is a Python package that can change the color of the console output and support various terminal (except for Windows cmd.exe).

The following text colors are supported:

Grey, red, green, yellow, blue, magenta, cyan, white

The following background highlighting is supported:

On_grey, on_red, On_green, On_yellow, On_blue, On_magenta, On_cyan, On_white

The following properties are supported:

Bold, dark, underline, blink, reverse, concealed

 1 #!/usr/bin/env python3 2 #-*-coding:utf-8-*-3 # @Time: 2018/4/29 16:49 4 # @Author: Yang 5 # @File: Colo red_termcolor_module.py 6 # @Software: Pycharm 7 import sys 8 from termcolor import colored,cprint 9 Text = colored (' Hello , world! ', ' Red ', attrs=[' reverse ', ' blink ']) #colored (text, Color=none, On_color=none, Attrs=none) # Available Tex T colors:13 # Red, green, yellow, blue, magenta, cyan, white.14 # Available text highlights:16 # on_re D, On_green, On_yellow, On_blue, On_magenta, On_cyan, on_white.17 # Available attributes:19 # Bold, Dark, und Erline, Blink, reverse, concealed.20 #print (' \033[5;7;31mhello,world!\033[0m '), print (text), Cprint (' Hello, world! ', ' green ', ' on_red ') #cprint (' hello,world! ', ' green ', ' on_red ', attrs=[' bold ')] #def cprint (text, Color=none , On_color=none, Attrs=none, **kwargs) Print_red_on_cyan = Lambda x:cprint (x, ' Red ', ' On_cyan ') Print_red_on_cyan ( ' hello,world! ') Print_red_on_cyan (' Hello,universe! ') For I in range: Cprint (i, ' magenta ', end= ') cprint (' attention! ', ' Red ', attrs=[' bold '],file = Sys.stderr)

Output Result:

Reference:

1, https://pypi.org/project/colorama/

2, https://pypi.org/project/termcolor/#description

3, https://www.cnblogs.com/hellojesson/p/5961570.html

4, https://stackoverflow.com/questions/287871/print-in-terminal-with-colors/3332860#3332860

Python Learning-terminal font highlighting 1

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.