Python - print函数 - 打印样式及颜色修改

依靠终端的显示机制,不需要安装其他支持库,拿来即可用

简单设置一下颜色和显示方式,就可以定义属于自己的color_print

color = {
    'b': 0,
    'r': 1,
    'g': 2,
    'y': 3,
    'blue': 4,
    'p': 5,
    'c': 6,
    'w': 7
}
method_lst = {
    'default': 0,
    'highlight': 1,
    'bold': 2,
    'non_bold': 22,
    'underline': 4,
    'non_underline': 24,
    'blink': 5,
    'reverse': 7
}


def color_print(s, method='default', front_color='w', back_color='b'):
    start = f'\033[{method_lst[method]};3{color[front_color]};4{color[back_color]}m'
    end = '\033[0m'
    print(start+s+end)


if __name__ == '__main__':
    s = "this is a test text\n"

    color_print(s, 'underline')
    color_print(s, front_color='c')
    color_print(s, 'highlight','r','y')




运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值