How to print in colours in the shell (PYTHON) - python-3.x

I am studying tech in Australia, and I am having trouble with getting this to print in colour. I would like to print the 'x's on the board in black (not blue), and so the numbers appear in blue ( in another part of the code). How do I print in black?
# creates the 'x's to display on the minesweeper board
for i in range(board_row):
board.append(["x"] * board_row)
ASCII and ANSI don't work, and I can't figure out how to get the modules like Blessings and colorama in to python

Related

Is there any way to draw large characters using ncursesw?

I want to create a main page for a program I'm trying to write and basically, I want to print the title at the center with a large font and menu and the rest after that. What I'm looking for is any character set in ncursesw or just anything which will help me draw large characters for title more precisely
For this (in ncurses), I tried A_REVERSE attribute and printed the respective places white, something like this
Large text
But other letters like z, g and t are hard to draw and they don't look good either.

Changing text color on output

I have written a text based game for the class I am taking, but I want to see how I can change certain output text to different colors. Say the villain loses, the text prints out red or something, and vice versa, if he wins, the text prints out in like blue. Do I need to define all the colors first, or is there a way to add it to the string? example:
print('Thank you for playing but unfortunately you lost your life!')
How can I change the output to red text?
is this python?
If so here is a link to a few options python colors
in your case I guess you want to have something like:
text = colored('Thank you for playing but unfortunately you lost your life!', 'red', attrs=['reverse', 'blink'])
print(text)
I'm basing my game off Game of Thrones. Here a sample of the output when the character loses. I'm still working on the story.
else:
print('\nOh dear! You did not collect all of the items!')
print('You were caught by Daenerys and set on fire by Drogon!')
print('Drogon escaped and set the township on fire!')
print('Thank you for playing but unfortunately you lost your life!')
print('You must start over')

How to print text with multiple different types of colors in he same line in Python 3?

I am trying to do some visualizing data on the terminal and I am doing lots of printing to do that. The issue I am having is that certain character symbols look the same. I figured that coloring them differently would help me see the differences. I see from this link that there is a way to do it, but I don't understand what I am looking at. There is no explanation for what is going on in those solutions.
How do you specify, in the same line, text with different colors?
I should also mention that I am building the printable string OUTSIDE of the call to print(). How do you build the colorful string outside of the print() call?
A rewritten form of my question:
1. Colorize the text I print to the string with multiple types of colors.
2. Colorize the string BEFORE it gets sent to the "print()" call.
A couple examples would be great.
Using colorama just like the answer you linked is doing:
from colorama import Fore, Style
my_str = f"{Fore.BLUE}Hello, {Style.RESET_ALL} guys. {Fore.RED} I should be red."
print(my_str)
This gives me:
As you can see Fore.<color name> changes the color of the text after it, until the Style.RESET_ALL. After that you can change the color of the text again.
There could be multiple ways to achieve this. One which doesn't require any extra packages is to use ANSI color codes. Look at this link. Below are some examples.
s = "\033[1;32;40m Bright Green on black \033[1;31;43m Red on yellow \033[1;34;42m Blue on green \033[1;37;40m"
print(s)
Here in first code \033[1;32;40m, \033[ is the escape code followed by 1 for bold, 32 for bright green text and 40 for black background. The 3 codes are separated by ; and ended with m. Adding all the 3 codes (1, 32 and 40 here) isn't mandatory though.
output:
Other ways to achieve this can be found here.

How to make a print statement to blink only once in Python 3?

I need to blink my print statement only once.
So now I have:
from termcolor import colored, cprint
blue = colored(', IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII', 'blue', attrs=['blink'], )
print(blue)
It keeps blinking , but I want it only once.
thank you
It's your terminal that makes text with certain attribute blink. You cannot control it. If you want to blink once, draw a text, wait, draw the text over the same position differently.

Printing matlab figures in color

When I try to print a matlab figure from red hat 5, it always comes out greyscale, even when I print to file. I can get around this by saving the plot as a PNG and then printing that, but I'd like to be able to print matlab plots directly.
I've tried selecting "color" in the print dialog box and in print preview, but the images still come out greyscale.
How can I get matlab to print in color?
You need to edit the printopt.m file on your system. Specifically, you need to add the line
dev = '-dpsc2';
This sets the printer driver to "color postscript", as opposed to the default printer driver for RHEL which is greyscale postscript.
See this page from the matlab documentation.

Resources