简体   繁体   中英

How do I make python 3.7 print out colored text?

I am new to programming as a whole, however I have been able to write up a basic game in python. I have not been able to print colored text into the powershell on windows 10, it's cmd, or in the IDLE python came with. Although it does not crash my code, I would like my game to print out colored text, so i need to make a method that can do that.

def ColorText(text, color):
  CEND      = '\033[0m'
  CBOLD     = '\033[1m'
  CRED    = '\033[91m'
  CGREEN  = '\033[32m'
  CYELLOW = '\033[33m'
  CBLUE   = '\033[34m'
  CVIOLET = '\033[35m'
  CBEIGE  = '\033[36m'
  if color == 'red':
      return CRED + CBOLD + text + CEND
  elif color == 'green':
      return CGREEN + CBOLD + text + CEND
  elif color == 'yellow':
      return CYELLOW + CBOLD + text + CEND
  elif color == 'blue':
      return CBLUE + CBOLD + text + CEND
  elif color == 'voilet':
      return CVIOLET + CBOLD + text + CEND
  elif color == 'beige':
      return CBEIGE + CBOLD + text + CEND

Color_Console library is comparatively easier to use. Install this library and the following code would help you.

from Color_Console import *
ctext("This will be printed" , "white" , "blue")

The first argument is the string to be printed, The second argument is the color of the text and the last one is the background color.

The latest version of Color_Console allows you to pass a list or dictionary of colors which would change after a specified delay time.

Also, they have good documentation on all of their functions.

Visit https://pypi.org/project/Color-Console/ to know more.

colorprint can help.

pip install color
from colorprint.printer import uprint
from colorprint.unicolor import *
uprint("FOREGROUND_GREEN\n", fore=FOREGROUND_GREEN)
uprint("BACKGROUND_WHITE\n", back=BACKGROUND_WHITE)

This repository supports as much as possible of the terminal supported.

https://github.com/sailist/colorprint

You can use clrprint module which works for idle, terminal and PowerShell too

pip install clrprint

Then use it with:

from clrprint import *
clrhelp() # print's available colors and usage

user_input = clrinput("input please: ",clr='r') # just like input() [color is red]
clrprint('your text',user_input,clr='green') # just like print() 

You can print out coloured text in windows cmd like this(changes colour of whole cmd):

import os
os.system("color 3") # colour can be any number between 1 to 8
print("Your text")

Or you can use colorama like this:

from colorama import init, Fore, Back, Style
init(convert=True)
print(Fore.RED + 'some red text') 
print(Back.GREEN + 'and with a green background') 
print(Style.DIM + 'and in dim text') 
print(Style.RESET_ALL) 
print('back to normal now') 

You can read this article on GeeksForGeeks which shows how to change colours in terminals: https://www.geeksforgeeks.org/print-colors-python-terminal/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM