Can you print text in colors other than blue using a print command in python 3.2? - python-3.x

My code currently only prints text in blue but I would like some important messages to be printed using the print() command but in colors such as red.
I tried using something called colorama which I found in a different question's answer.
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED+'Hello')
But it came up with this:
Traceback (most recent call last):
File "E:/Color test", line 1, in <module>
import colorama
ImportError: No module named colorama
Does anyone know how to do it or at least know if it is possible?
EDIT: I have found a similar question and tried all of the answers but none of them worked. I will really appreciate anybody's help.

You can do this using ANSI escape codes.
Black: \u001b[30m
Red: \u001b[31m
Green: \u001b[32m
Yellow: \u001b[33m
Blue: \u001b[34m
Magenta: \u001b[35m
Cyan: \u001b[36m
White: \u001b[37m
Reset: \u001b[0m
Suppose you want to print "Hello world" in blue, You will have to do:
print("\u001b[34m" + "Hello world" + "\u001b[0m")
Please note that I have added the reset code at the end to avoid coloring the rest of the text.
Additionally, if you are using a windows operating system you will need to clear the screen for the colors to appear, like this:
import os
os.system("cls")
Good luck!

There is a way - but that it is much more difficult than what I can just simply put in here and is a lot more advanced. is it possible to add colors to python output? Go to that weblink for more info.

Related

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.

Printing colored texts in Powershell, using python

I used termcolor to print a colored text which ended up printing a weird code in Powershell, thus I used colorama.init() but it didn't change anything. I copied and texted some other code from Stackoverflow as I failed to import colorama at the beginning.
import sys
from termcolor import colored
if sys.platform == 'win32':
try:
import colorama
except ImportError:
pass
else:
colorama.init()
text = colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"])
print(text)
This codes prints out a colored message in Powershell (it doesn't blink though). However I don't know why this shorter code doesn't work. (It prints "[5m[46m[35mHI THERE![0m")
from termcolor import colored
import colorama
colorama.init()
text = colored("HI THERE!", color="magenta", on_color="on_cyan", attrs=["blink"])
print(text)
I do import termcolor and colorama; and use init, colored methods in both these codes but I don't know why it only works with the first code. Do you know what might be the reason behind this? Thanks in advance.

Creating a Retro-themed loading screen in Python 3.x

I have an idea to include a loading screen, just for a fun project I'm working on, prior to starting the actual program in Python.
Back in the 80s, we had loading screens on the ZX Spectrum, and other 8-bit computers, that displayed an image one line at a time, then coloured the image in - something to look at while the game loaded. You can find an example at: https://youtu.be/MtBoRp_cSxQ
What I'd like to do is part-replicate this feature. I'd like to be able to take a JPG or PNG and have Python code to load it one line at a time, much in the same as the video in the link above. I don't mind if it can't be coloured in after, or that there's no funky raster bars in the borders. Just an image, 'loading' in at one line at a time (if you see what I mean).
I can achieve much the same effect in the console with some basic code, using a text file with ASCII art, but it'd be great with an actual image.
Any help will be greatly appreciated.
As always, thanks in advance.
Dave.
import os
import time
def splash_screen(seconds):
splash=open("test.txt", 'r')
for lines in splash:
print(lines)
time.sleep(seconds)
splash.close()
#Main Code Start
os.system('cls' if os.name == 'nt' else 'clear')
splash_screen(.5)
username=input("Type your username:")

How to change Python background to a certain colour with colorama?

I am using colorama to change the colour of my text and it works fines, but now I want to change the colour of the whole background instead of just my text.
I am using Windows:
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Back.GREEN)
print(Fore.RED)
But, that code only makes the text coloured. Is there a way to do that in python? I want it like CMD where you can have the background of it a colour. I cannot use the OS module as I do not have admin rights, but I'm open to using any other module.
How do I solve this problem?
After a while in playing with it i figured it out. Just forgot about this post. Here is what I did.
import colorama
from colorama import Back as bg
colorama.init()
print(bg.RED)
print(colorama.ansi.clear_screen())
I think clearing the screen fixed the Issue
After playing with colorama on my Windows 10 box it seems it's only used to change the text but not the console/terminal background. I was however able to change the background using this standard library solution:
import ctypes
try:
ctypes.windll.msvcrt.system(b"color 4f")
print("Hello World!")
except ValueError:
pass
The terminal background will change to red with white text.

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.

Resources