python print输出有颜色的字体

如题

def log(str, color="red", background="default", effect="default"):
    """ 颜色日志
            # \ 033[显示方式;字体色;背景色m......[\ 033[0m]
    """
    #  黑色、红色、绿色、黄色、蓝色、紫红、靛蓝、白色
    _color = \
        {"black": "30", "red": "31", "green": "32", "yellow": "33", "blue": "34", "purple-red": "35",
         "cyanine": "36",
         "white": "37"}[color]
    #  黑色、红色、绿色、黄色、蓝色、紫红、靛蓝、白色
    _background_color = \
        {"black": "40;", "red": "41;", "green": "42;", "yellow": "43;", "blue": "44;", "purple-red": "45;",
         "cyanine": "46;",
         "white": "47;", "default": ""}[background]
    #  默认、高亮、下划线、闪烁、反白、不显示
    _effect = \
        {"default": "0", "highlight": "1", "underline": "4", "flash": "5", "backwhite": "7", "unshow": "8"}[
            effect]

    print('\033[%s;%s%sm %s \033[0m!' % (_effect, _background_color, _color, str))

测试代码
_color = \
        {"black": "30", "red": "31", "green": "32", "yellow": "33", "blue": "34", "purple-red": "35",
         "cyanine": "36",
         "white": "37"}
    _background_color = \
        {"black": "40;", "red": "41;", "green": "42;", "yellow": "43;", "blue": "44;", "purple-red": "45;",
         "cyanine": "46;",
         "white": "47;", "default": ""}
    #  默认、高亮、下划线、闪烁、反白、不显示
    _effect = \
        {"default": "0", "highlight": "1", "underline": "4", "flash": "5", "backwhite": "7", "unshow": "8"}
    for color in _color:
        for bk in _background_color:
            for effect in _effect:
                log("hello world",color=color, background=bk, effect=effect)
效果截图

hello world

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值