zoukankan      html  css  js  c++  java
  • python print 输出带颜色字体

    实现过程

    终端的字符颜色是用转义序列控制的,是文本模式下的系统显示功能,和具体的语言无关

    转义序列是以ESC开头,即用33来完成(ESC的ASCII码用十进制表示是27,用八进制表示就是033)

    书写格式

    开头部分:33[显示方式;前景色;背景色m + 结尾部分:33[0m

    注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,可以只写其中的某一个;另外由于表示三个参数不同含义的数值都是唯一的没有重复的,所以三个参数的书写先后顺序没有固定要求,系统都能识别;但是,建议按照默认的格式规范书写

    对于结尾部分,其实也可以省略,但是为了书写规范,建议33[***开头,33[0m结尾

    数值表示的参数含义:

    显示方式: 0(默认)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、 5(闪烁)、25(非闪烁)、7(反显)、27(非反显)

    前景色: 30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋 红)、36(青色)、37(白色)

    背景色: 40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(洋 红)、46(青色)、47(白色)

    懒人代码示例(方便使用,即粘即用)

    print("显示方式:")
    print("33[0;37;40m--- Replace the string ---33[0m")
    print("33[1;37;40m--- Replace the string ---33[0m")
    print("33[22;37;40m--- Replace the string ---33[0m")
    print("33[4;37;40m--- Replace the string ---33[0m")
    print("33[24;37;40m--- Replace the string ---33[0m")
    print("33[5;37;40m--- Replace the string ---33[0m")
    print("33[25;37;40m--- Replace the string ---33[0m")
    print("33[7;37;40m--- Replace the string ---33[0m")
    print("33[27;37;40m--- Replace the string ---33[0m")
    print("前景色:")
    print("33[0;30;40m--- Replace the string ---33[0m")
    print("33[0;31;40m--- Replace the string ---33[0m")
    print("33[0;32;40m--- Replace the string ---33[0m")
    print("33[0;33;40m--- Replace the string ---33[0m")
    print("33[0;34;40m--- Replace the string ---33[0m")
    print("33[0;35;40m--- Replace the string ---33[0m")
    print("33[0;36;40m--- Replace the string ---33[0m")
    print("33[0;37;40m--- Replace the string ---33[0m")
    print("背景色:")
    print("33[0;37;40m--- Replace the string ---33[0m")
    print("33[0;37;41m--- Replace the string ---33[0m")
    print("33[0;37;42m--- Replace the string ---33[0m")
    print("33[0;37;43m--- Replace the string ---33[0m")
    print("33[0;37;44m--- Replace the string ---33[0m")
    print("33[0;37;45m--- Replace the string ---33[0m")
    print("33[0;37;46m--- Replace the string ---33[0m")
    print("33[0;37;47m--- Replace the string ---33[0m")
    

    因为隔一段时间不用就忘,这样整一下子用起来方便。

    此处为原博文传送门, 支持正版

  • 相关阅读:
    C程序的存储空间布局
    获取系统数据文件信息
    基于UDP的一对回射客户/服务器程序
    一段经典的 Java 风格程序 ( 类,包 )
    Vue 脱坑记
    vue面试题总汇
    JavaScript调试技巧
    伪元素小技巧
    JavaScript 开发人员需要知道的简写技巧
    select2插件改造之设置自定义选项 源码
  • 原文地址:https://www.cnblogs.com/shu-sheng/p/13232238.html
Copyright © 2011-2022 走看看