In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how Python in the terminal color printout related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you read this Python how in the terminal color printout article will have a harvest, let's take a look at it.
1. Introduction
Creating a command line program is great: the command line can do the job as we set it up, and it doesn't take hours to try to make GUI work properly compared to the GUI interface program. But sometimes it's best to have a more attractive program, and it's not possible to use ordinary printing functions directly.
A good compromise is to use the colorama library. This makes it very easy to color the output strings on the terminal and improve the interface appearance of the program.
Cut the gossip, and then let's see how to use it.
two。 working principle
The implementation behind this library is very simple, and it usually uses ANSA escape character sequences. When the terminal reads one of these sequences, it does not output. When the terminal is indicated as the next output, it uses the previously set color for the corresponding output.
For example, the string\ 033 [92m represents the color green, and the string\ 033 [0m] is used to set the color of the terminal to the standard default color (usually black). The sample code is as follows:
GREEN ='\ 033 [92m'END_COLOR ='\ 033 [0m'print (GREEN + "Hello World" + END_COLOR) print ("Hello word 2022!")
The output of the above code is as follows:
3. Use the Colorama library
Now that we understand how the above library works, let's use it. First, install it or use pip to install it directly. The command line is as follows:
Pip install colorama
After the installation is complete, then we can start coding. Adding the following initialization code at the beginning of the script is a good way to write it programmatically (such as displaying a call to the init function on windows systems):
From colorama import initinit ()
This is used to ensure that the Windows command line handles the sequence of ANSA strings correctly. Otherwise, it will only print out a sequence of characters.
If you are using another operating system, the init () function will do nothing at this time, but it is recommended that you should still explicitly put it into our program to ensure that our program works on any platform.
Now we can start using this library. It is mainly divided into three subcategories:
Fore for changing the color of the output text, Style for changing the brightness of the output text, and Back for changing the background of the output text (that is, highlighting the text).
4. Change the color of the output text
Then we can start by changing the color of the text. Colorama allows us to use eight different colors: black (black), red (red), green (green), yellow (yellow), blue (blue), magenta (magenta), cyan (cyan), and white (white). They are implemented as variables in the Fore class. Their names are the names of colors, all capitalized. For example:
From colorama import Fore, initinit () print ('Now is not colored') print (Fore.RED +' some red text') print (Fore.GREEN + 'some green text') print (Fore.MAGENTA +' some magenta text') print (Fore.RESET + 'Back to normal')
The output is as follows:
The above code is easy to implement, and as we can see, we also use another variable, RESET, which is mainly used to restore the original color of the text.
5. Change the background of the output text
The class we will introduce next is Back, which implements the same nine keywords as the Fore class: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET.
In this context, the color you set will be used to change the background of the output text (that is, highlighted text). Examples are as follows:
From colorama import Back, initinit () print ('Now is not highlighted') print (Back.RED +' some red background') print (Back.GREEN + 'some green background') print (Back.MAGENTA +' some magenta background') print (Back.RESET + 'Back to normal')
The output is as follows:
6. Change the brightness of the output text
Finally, we can use the Style class to change the brightness of the output text. This subclass contains the following three main keywords:
BRIGHT brightens the output text DIM darkens the output text (although it looks the same as normal text) the output text of NORMAL becomes normal brightness
Examples are as follows:
From colorama import Style, initinit () print ('Normal text') print (Style.BRIGHT +' Bright text') print (Style.NORMAL + 'Normal text')
The sample output is as follows:
In addition, the class implements the RESET_ALL keyword, which resets everything (brightness, color, background) to normal values. Examples are as follows:
From colorama import Fore, Back, Style, initinit () print (Style.BRIGHT + 'Now the text is bright') print (Fore.RED +' Now the text is bright and red') print (Back.GREEN + 'Now the text is bright, red and with green background') print (Style.RESET_ALL +' Now everything is back to normal')
The output is as follows:
This is the end of the article on "how to print Python on the terminal". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to print Python on the terminal". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.