How to modify the format when printing?

122 Views Asked by At

My goal is to be able to change the default format when printing in Python.

With the following code, I can change color, bold and centering. I would also like to change the font size and even the font style but I don't know how.

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

There are 1 best solutions below

0
Timothée On

To add color, you can also use :

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}")

or :

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'))