This question already has answers here:
Python: How can I make the ANSI escape codes to work also in Windows?
(11 answers)
Closed last month.
ANSI code wont work on my python interpreter
I wanted to color some of my prints on the project. I looked up on how to color printed characters and found the ANSI escape codes, so i tried it up on the interpreter, but it wont work.
for example:
print("\033[32m Hello")
it Gives me <-[32m Hello (an arrow left sign).
how do i make it work? can it work on python's interpreter? if not, where should i use it?
Note, this is a possible duplicate to this question answered by this post. A reiteration of the answer by #gary-vernon-grubb is posted below for convenience.
Use os.system('') to ensure that the ANSI escape sequence is processed correctly. An example in the Windows Command Prompt can be seen below:
Ensure that there are no spaces between the ANSI escape sequence and the color code! This was a bit of a pain in the neck for me.
You are best off installing other packages to help generate the ANSI sequences, iirc win32 console does not support ANSI colours natively. One option is to install colorama. The following snippet prints out red text.
import colorama
from colorama import Fore, Back, Style
colorama.init()
print(Fore.RED + 'This is red')
Edit: Upon researching a little more, I've realised that Windows 10 has support for ANSI Escape Sequence and you can do so by calling. Use this if you intend on copy and pasting.
import os
os.system("echo [31mThis is red[0m")
However i'd still prefer the former.
You could be using IDLE... in that case you can't have ANSI colours; the IDLE 'terminal' isn't really a terminal so ANSI codes will show up as a character, whether you type chr(0x1B) or \033 or \x1b; It's all the same.
Your arrow character is normal; I just get a box because I guess the default font doesn't support left arrows...?
But #thatotherguy's explanation might be right... unless you're using IDLE because in that case it's definitely the problem.
Related
How could I make a word in giant text (for example “welcome” but giant)? I know that it's possible to change the console to make the font larger but I need it for just the word.
There are 2 ways to solve this problem
1 - Change the font in the python shell. Go to configure IDLE , go to the fonts tab and change the size value. Then apply the changes
2 - Using ASCII art. You can use ASCII art generators or use the python package pyfiglet(python version of figlet).
Example with pyfiglet
import pyfiglet
result = pyfiglet.figlet_format("Hello World")
print(result)
Pyfiglet also allows you to use many fonts , you can read their documentation for the everything else.
Hope this helps :)
I'm using VSCode Version: 1.46.1 on Mac OS Catalina. I'm using the built-in Python interactive terminal Python 3.7.4 Whenever I print strings, it shows up with unicode, making it difficult to read, like so:
\\u201cI like what we have.\\u201d It is quiet and there is somebody else in the room. I tell my dog that I need to go and he says, \\u201cjust alright.\\u201d ~~I am hungry.\\n\\n
I have tried every flavor of un-escaping escaped characters. See here:
Unescaping escaped characters in a string using Python 3.2
And
Using unicode character u201c
But to no avail. I think the problem lies in the encoding options built into VSCode itself, but I'm not sure how to modify that.
Maybe this page could provide some information for you.
"\u201c" and "\u201d" means “ and ”, but they will not work, they should be "\u201c" and "\u201d".
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
Please don't mark my question as already answered, because in all of the questions on stackoverflow or in the Unicode HOWTO I can't figure out how to print the overline or U+203E character in Python 3. Can someone please explain in baby programmer language how to print unicode characters like this one? I have tried some things, but to be honest I had no idea what I was doing.
I am working Kubuntu xenial (16.04).
When I try to print the character I get a UnicodeEncodeError. My question would be, how to work around this error?
EDIT 1: Problem located
I have now figured out my locale is set to POSIX, which would be ASCII encoding. I will try to set it to UTF-8 encoding.
EDIT 2: Still no solution
I have found out what I need to change, I just haven't found out how to. For anyone with the same issue, there's a comment with a link to a post where a similar problem is solved.
EDIT 3: Final answer
Here is a link to an askubuntu forum where I asked how to edit my /etc/default/locale file. Turns out one command in the Linux shell was enough. For me a lot of stuff doesn't work, but this command allowed me to set my locale to en_US.UTF-8: sudo /usr/sbin/update-locale LANG=en_US.UTF-8. After rebooting my OS, the settings had applied and my locale was changed.
Now I don't need the overline character anymore, because I have learned to work with graphics libraries, but I have had multiple problems because of my locale. Thanks to everyone for the advice!
Use \u to indicate a unicode character: print("\u203e").
You need to use the combining character U+0304 instead.
print(u'a\u0304')
ā
U+0305 is probably a better choice (as viraptor suggests). You can also use the Unicode Roman numerals (U+2160 through U+217f) instead of regular uppercase Latin letters, although (at least in my terminal) they don't render as well with the overline.
print(u'\u2163\u0305')
Ⅳ̅
print u'I\u0305V\u0305'
I̅V̅
It seems like your sys.stdout.encoding is 'ascii'.
Try to set it to 'utf-8':
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.detach())
print ('\u203e')
print(u' \u0305'*k)
with k as the length of the line in characters.
e.g.
print(u' \u0305'*5)
Is there a way to specify the color a text is printed within Idle for Python 3.2?
I'm looking for something like:
print("foo", "#fafafa")
print("bar", "#4f4f4f")
http://docs.python.org/library/idle.html#syntax-colors
To change the color scheme, edit the [Colors] section in config.txt.
Edit: As you've edited your question, here is an edited answer. See
http://www.daimi.au.dk/~mailund/scripting2005/lecture-notes/process-management.html
for how to use terminal escape sequences in Python to change the color of things.
Will they work in IDLE? I don't know. But they will work in most terminals.