current position:Home>Pythonnote032 --- Python print colored string

Pythonnote032 --- Python print colored string

2022-07-25 15:08:27Weigtang 406 team

Intro

   There is a lot of information on the Internet , Not much to say , There are other ways . Record the small functions encapsulated by yourself , Convenient for your own use .

Code

def magic_str(raw_str, display_mode="", font_color="", back_color=""):
    """ jupyter Color can be displayed when printing  :param raw_str:  Original string  :param display_mode:0 Default 1 bold 4 Underline 5 flashing 7 On the contrary  :param font_color: Font color -30 black 31 red 32 green 33 yellow 34 blue 35 purple 36 green 37 white  :param back_color: Background color -40 black 41 red 42 green 43 yellow 44 blue 45 purple 46 green 47 white  :return: """
    display_str = "" if display_mode == "" else display_mode + ";"
    font_str = "" if font_color == "" else font_color + ";"
    back_str = "" if back_color == "" else back_color + ";"
    str_mode = display_str + font_str + back_str
    str_mode = str_mode[0:-1]
    res = '\033[' + str_mode + "m" + raw_str + " \033[0m"
    return res

print(magic_str("test",display_mode="1",font_color='31')+" test")

 Insert picture description here

Ref

[1] https://stackoverflow.com/questions/287871/how-to-print-colored-text-to-the-terminal

                              2021-09-14 Jiulong lake, Jiangning District, Nanjing

copyright notice
author[Weigtang 406 team],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/206/202207251501086412.html

Random recommended