print("Player 1 is up")`
print("Player 2 is up")`
I would like to be able to print the text in the console as a different color for each player, I've tried a couple of the imports, but it doesn't work. Any help would be gratefully received.
As shown below my attempt at trying one of the suggestions on the "How to print colored text in python. I have tried a lot of ways, but they all print the ANSI escape sequence.
You have to add the code for the color you want displayed. For instance: Let's say i want to print out the text "This is not white" with a pink outline. Then by using the print-function, i first add the color for pink and then the text i want displayed within the print-function. The code for pink is: \x1b[1;45m
So adding the two together:
print('\x1b[1;45m' + 'This is not white.')
It is kind of like adding to string-values together in a print-statement.
The output will be "This is not white" in a pink outline.
Related
I'm trying to change the background colour of a space (" ") to red using the ANSI escape sequence.
print('\u001b[41m')
This changes the background of the entire line to red. How can I make it stop after one space?
You can use rich to simplify how to send the sequences
from rich import print
print('[red reverse] [/red reverse]other text')
I want to use bold text for the labels of my python plot. I tried this:
plt.xlabel("$\mathrm{\\mathbf{\delta\\langle r^2\\rangle^{226,A}[fm^2]}}$",fontsize=50,fontweight='bold')
But the output looks weird (see the attached picture). The letters get bold, but the numbers and the symbols stay the same. How can I get everything in bold? Thank you!
Using weight instead of fontweight.
Update:
I found that using \boldsymbol will work with the Greek letter.
\boldsymbol{\mathbf{text}}
I am very new to Python, and wanted to learn how to print coloured text. I don't have any modules like termcolor or colorama, but I found that I can use ANSI escape sequences to do this.
I found a sample code:
print('\x1b[6;30;42m' + 'Success!' + '\x1b[0m')
However, when I run it, it just prints the entire thing like this:
[6;30;42mSuccess![0m
What am I doing wrong, and how can I print coloured texts?
Thank you very much!!
Edit: I am using python 3.7
So it turned out you were doing nothing wrong, just executing the Python code in an environment (the IDLE IDE on Mac) which does not recognize (by default?) ANSI escape sequences.
On Mac, ANSI escape sequences are supported, e.g., by Terminal.
Try this
Blue = '\033[34m' # Blue Text
print(Blue + 'Hello World')
at the spot where it says 34m that is the colour. red is 31
yellow is 33 purple is 35. and more. THis works for python
How to highlight a word/letter in a text by changing its color\colour using python 2.7?
try:
using clint.
>>> from clint.textui import puts, colored
>>> puts(colored.red('Text in Red'))
Text in Red
but i want to color only the 'x' in the 'Text' for example.
import termcolor
string = 'Text in Red'
string = string.replace('x', termcolor.colored('x', 'red'))
print string
The following will work.
>>> from clint.textui import puts, colored
>>> puts('Te'+colored.red('x')+'t in Red')
Let me explain why this works by first explaining how colors are displayed in the console.
When you want to tell the console to change colors you would think you would have to do some special system call or something, but all you have to do is output some special characters called an ansi escape sequence. Clint handles this for you. When you used the clint.ansi.red function, the escape character for red was added before the x and the escape character to reset everything back to normal was added after the x.
This means that, 'Te'+colored.red('x')+'t in Red' is the same thing as 'Te\x1b[31m\x1b[22mx\x1b[39m\x1b[22mt in Red', and you can continue to add text in others colors to your hearts content.
Also, clint handles this, but just for informational purposes, if you want to see the full list of color escape sequences they can be found here. There are other ansi codes that allow you to do things like changing the position of the cursor. You can find a list of all of the ansi codes here. If you mess with ansi codes outside of the clint library, make sure you're taking into account every flow of control including unexpected exceptions that need will need to revert the text back to normal. It's no fun to execute a program, run into some errors that turn the console red, and then have the program exit, but you're still left with a red console.
Here's a non-external library approach which I tend to use a lot
class bcolors:
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
CYAN = '\033[96m'
WHITE = '\033[97m'
YELLOW = '\033[93m'
MAGENTA = '\033[95m'
GREY = '\033[90m'
BLACK = '\033[90m'
DEFAULT = '\033[99m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
print(bcolors.WHITE + foo + bcolors.END)
print(bcolors.GREEN + bar + bcolors.END)
I'm looking to make a game in python's Shell. However, when text is printed, it is printed at the bottom. Is there any code I can use to scroll it all to the top or indent it. Please let me know! I'll show an example of what I want to happen:
The code sections are the Shell screens
EDIT
This 'Hello Welcome' is printed at the top of the Shell screen
Hello
Welcome!
This is an example for a UI, the text is indented:
Money: $5
The text is indented
I also want to select a line to print the text eg:
Line 1
Line 2
Line 3
print 'Hi' line 2
If anyone knows how to do this please let me know, I've looked for some time and haven't found any answer.
Use formatting for indentation
print(' %s' % 'hello')
>>> hello
The perfect solution and, as I know common for console apps: clean output and reprint it with updated state