栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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'33[{method_lst[method]};3{color[front_color]};4{color[back_color]}m'
    end = '33[0m'
    print(start+s+end)


if __name__ == '__main__':
    s = "this is a test textn"

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




运行结果:

转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/1038113.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号