The method of printing color information in Python

In Python, you can use the print() function to print out color information. Before using print() to print, you need to call the os standard library to set up the system.

1 os standard library

1.1 Introduction

os is the abbreviation of Operating System, that is, "operating system". The os standard library is an operating system interface module that provides functions for using operating system-related functions.

1.2 Import of os standard library

The library needs to be imported through import, and the code is as follows:

import os

1.3 Judgment of operating system type

The type of the operating system can be judged by os.name, where posix indicates that the operating system is Linux; nt indicates that the operating system is Windows; java indicates that the operating system is a java virtual machine. So the code for operating system type judgment is as follows:

if os.name == 'nt':
    os.system('')

1.4 Creation of child processes

When the current operating system is determined to be Windows by os.name, a new subprocess needs to be created to display color information, and the task of creating a subprocess is completed by os.system(). The os.system() function can create a subprocess and convert the string into a command to run in this subprocess. If the parameter of this function is empty, it means that only the subprocess is created without executing the command.

2 print() function to print color information

When a child process is created through the os annotation library, the color information can be printed through the print() function.

2.1 Format for printing color information

The format for printing color information using print is as follows:

\033[显示方式;前景色;背景色m 显示内容 \033[0m

Among them, \033 is an escape sequence, the value is an octal number, converted to decimal is 27, which is the ASCII code value of ESC.

2.2 Print a single line of color information

Use the following code to print a single line of color information:

print('\033[1;37;41m--------我是彩色信息--------\033[0m')

Among them, the number 1 behind the square brackets is the display mode, which means highlighting; 37 is the foreground color, which means white; 41 is the background color, which means red.

Related link 1 Display mode value and description

Display method

illustrate

Display method

illustrate

0

Terminal default settings

1

Highlight

4

use underscore

5

flashing

7

Highlight

8

Invisible

Related link 2 Corresponding value of foreground color and background color

color

foreground color

background color

color

foreground color

background color

black

30

40

red

31

41

green

32

42

yellow

33

43

blue

34

44

Fuchsia

35

45

cyan

36

46

White

37

47

2.3 Running the program

The program cannot display the effect of color information in IDLE, and needs to be run in the cmd window. After opening the cmd window, drag the program file into the cmd window, and the absolute path of the Python file will be automatically displayed in the cmd command line, as shown in Figure 1①. After clicking Enter, the color single-line information will be displayed, as shown in Figure 1②.

Figure 1 shows a single line of color information

2.4 Print multi-line color information

Use the following code to print multiple lines of color information.

print('\033[1;37;41m')
print('--------我是彩色信息1--------')
print('--------我是彩色信息2--------')
print('--------我是彩色信息3--------')
print('\033[0m')

Among them, the first line is the start mark for printing color information, and the last line is the end mark. The effect after running the file is shown in Figure 2.

Figure 2 Printing multiple lines of color information

Guess you like

Origin blog.csdn.net/hou09tian/article/details/130945294