【问题标题】:How to modify the format when printing?打印时如何修改格式?
【发布时间】:2021-03-09 16:58:11
【问题描述】:

我的目标是在 Python 中打印时能够更改默认格式。

使用以下代码,我可以更改颜色、粗体和居中。我也想更改字体大小甚至字体样式,但我不知道如何。

from termcolor import colored
print(colored('Hello'.center(100), 'green', attrs=['bold']))

【问题讨论】:

    标签: python python-3.x termcolor


    【解决方案1】:

    要添加颜色,您还可以使用:

    class bcolors:
        HEADER = '\033[95m'
        OKBLUE = '\033[94m'
        OKCYAN = '\033[96m'
        OKGREEN = '\033[92m'
        WARNING = '\033[93m'
        FAIL = '\033[91m'
        ENDC = '\033[0m'
        BOLD = '\033[1m'
        UNDERLINE = '\033[4m'
    
    print(f"{bcolors.WARNING}Warning: No active frommets remain. Continue?{bcolors.ENDC}")
    

    或:

    def colored(r, g, b, text):
        return "\033[38;2;{};{};{}m{} \033[38;2;255;255;255m".format(r, g, b, text)
      
    text = 'Hello, World'
    colored_text = colored(255, 0, 0, text)
    print(colored_text)
    #or
    print(colored(255, 0, 0, 'Hello, World'))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多