This question already has answers here:
How do I print colored text to the terminal?
(64 answers)
Closed 9 years ago.
I'm looking for a way to change the color of the text output from my python scripts as it runs. The basic idea is something like this:
if (Data < LowerLimit):
print "Failed" # Output Failed as Red Text
elif (Data > UpperLimit):
print "Failed" # Red Color
else:
print "Passed" # Blue Color
The scripts are being used on windows machines for quick data analysis.
Or about the best module I have found
http://pypi.python.org/pypi/colorama
This is extremely simple! Rather than importing odd modules for python or trying long commands you can take advantage of windows OS commands.
In windows, commands exist to change the command prompt text color. You can use this in python by starting with a: import os
Next you need to have a line changing the text color, place it were you want in your code.
os.system('color 4')
You can figure out the other colors by starting cmd.exe and typing color help.
The good part? Thats all their is to it, to simple lines of code.
-Day
Try to look at the following link: Python | change text color in shell
Or read here: http://bytes.com/topic/python/answers/21877-coloring-print-lines
In general solution is to use ANSI codes while printing your string.
There is a solution that performs exactly what you need.
Been looking into this for a while and not got any satisfactory answers, however...
1) ANSI escape sequences do work in a terminal on Linux
2) if you can tolerate a limited set of colo(u)rs try this:
print("hello", end=''); print("error", end='', file=sys.stderr); print("goodbye")
In idle "hello" and "goodbye" are in blue and "error" is in red.
Not fantastic, but good enough for now, and easy!
Related
Very new to python, so ignorance may be shown. (Python 3.10, Windows 10)
I have a simple code that tosses a coin. I want to underline the 'y' and 'n' in "yes" and "no" within this sentence: Do you want to flip another coin? yes or no.
I was able to make it work properly in IDLE, but it doesn't work properly when running in windows. The underline positions shifts to the left in Windows. I assume this is an issue in Windows, but can it be fixed? I'm simply double clicking the python file to run it in Windows, so maybe this is an incorrect way of doing it?
Here is the code for the sentence:
Yes = ('''es''')
No = ('''o''')
print('Do you want to flip another coin? ' + '\u0332y'+Yes + ' or' + ' \u0332n'+No)
Thanks for the help.
I have the solution,
follow the below code,
from prompt_toolkit import print_formatted_text, HTML
print_formatted_text('Do you want to flip another coin? ',HTML('<u>y</u>es'),'or',HTML('<u>n</u>o'))
As given in an answer on Print in terminal with colors using Python? , I am trying to print in color on console/terminal using following code:
RED = "\e[31m"
NORMAL = "\e[0m"
print("TESTING")
print(RED+"TESTING"+NORMAL)
print("TESTING")
However, it is not working and only giving following output:
TESTING
\e[31mTESTING\e[0m # IN BLACK, THOUGH IT IS SHOWING COLOR HERE.
TESTING
Where is the problem and how can it be solved? I am using Python version 3.5.3 on Debian Stable Linux.
You have found a wrong answer; \e is not a valid escape sequence in Python. In some echo implementations, \e is an escape sequence for the ASCII ESC character, but in Python you need to use a different notation.
The rest of the answers on that page use correct forms, either \x1b or \033.
This question already has answers here:
Python, writing multi line code in IDLE
(7 answers)
Closed 6 years ago.
I just picked up a basic book today on programming. The coding language is Python and I have only been trying this out for a few hours but I'm already stuck, because I can't figure out how to write multiple lines of code. For example, when I write print("one") and then hit enter, it just runs it and prints the word, one. How can I have it print the word, one, and then the word, two, on the line below it? Also, when I hit tab it just moves over 4 spaces, or so. I can't figure out how to have it not run the first command, and just give me '>>>' on the next line. So I guess what I'm asking is: What keystrokes do I need to use to get something like:
>>> print("one")
>>> print("two")
Thanks so much!
(Sorry for such a basic question, but I'm totally confused on this one.)
The Python REPL automatically executes each command as soon as it is completely typed in. This is why it is called a "read-eval-print loop". It accepts one input, evaluates it, and then prints the result.
If you want to execute two complete commands at once, you can put a semicolon between them, like this:
print("one"); print("two")
I said "completely typed in" above, because some commands inherently require multiple lines, so Python must accept several lines of input before the command is "completely typed in". Three types of command work like this: flow-control commands (def, while, if, for, etc., which apply to several indented lines below them), multiline expressions (calculations inside parentheses or brackets), or statements that use a backslash (\) at the end of the line to indicate that it is continued on the next line. So if you type in any of the blocks below, Python will wait until the block is completely finished before evaluating it.
if 1 + 1 == 2:
print "True"
else:
print "False"
print(
1 + 1
)
print \
1 + 1
You could also combine these two strategies and type something like this:
print("one"); \
print("two")
Python will wait for both commands to be typed and then run them both at once. But I've never seen anyone write code that way.
Alternatively, you could type several commands together in a different text editor, and then paste them into the Python REPL, e.g., copy and paste the following into your REPL (but you will get results printed between the commands):
print("one")
print("two")
Alternatively, you can probably get almost exactly the behavior you were originally expecting by using a different interface to Python. The IPython Notebook is a good choice, or you could try the Spyder or PyCharm editors, which let you select a few lines of code and run them.
Or, if you have a longer script that you want to run all at once, the best option is to type it up in a text file (e.g., script.py), and then tell python to run it, e.g., type python script.py from a system command prompt (not the Python interpreter), or press F5 in the IDLE editor.
One thing you may want to try is writing your code in a file, say learning.py, and then running that file on the command line with python learning.py.
The best way to get better support for multi line commands in python with a "console" feel is to use ipython qtconsole, or Jupyter qtconsole as its now called: http://jupyter.org/qtconsole/stable/. When using qtconsole, hitting Ctrl-Enter will delay the command from running even if it's not a complex block. You can keep hitting Ctrl-Enter as many times as you want, and then hit Enter to run them all. Hitting up arrow will then bring up the whole block again to edit, cleanly indented unlike the regular ipython console.
Note: this is not ipython notebook, nor the regular ipython console, but a separate thing from either using the same kernel. The qtconsole has some other nice things like better syntax highlighting and inline plotting compared to the terminal.
In Learn Python The Hard Way (Exercise 13) the 3rd Study Drill says to "Combine raw_input with argv to make a script that gets more input from a user."
I wrote this script below, intending to have the terminal prompt the user for answers to three questions, then it would print back phrases with those answers integrated into them. However, I get an error about not having enough values to unpack when I try to run it with the following command:
python ex13.py
I understand that I need more variables to unpack in order for the script to work, so when I type this then the script works but never outputs the variables "first", "second" or "third" (which I don't want it to anyway):
python ex13.py first second third
I know how to write a script without importing argument variables, but how else can I interpret the study drill? I know I am not understanding the prompt of the study drill correctly but I'm not sure how to write the script differently or even if I am going in the right direction.
Can anyone offer some tips or advice? You don't have to give me the answer outright (I like figuring things out) but I am at a loss for the moment.
MY SCRIPT:
from sys import argv
script, color, number, shape = argv
color = raw_input("What is your favorite color? ")
number = raw_input("What is your favorite number? ")
shape = raw_input("What is your favorite shape? ")
print """
This program is called %r and it will determine your
favorite color, number and shape.
""" % script
print "Based on your answers, your favorite color is:", color
print "Your favorite number is:", number
print "And your favorite shape is a:", shape
What exactly do you want your code to do? If you want to have
$ python ex13.py
$ What is your favorite color? <yourColor>
..........
$ Your favorite color is <yourColor>
Then you need to get rid of the part where you set all those values from argv. argv is a list of the arguments passed to python when you invoke it in the command line. The fix you have in your comments sets script = ['ex13.py'] instead of 'ex13.py' for precisely this reason, you're setting script to be a list as opposed to a string.
If you want your code to run so that you pass the script arguments when you run it, you could get rid of your sections calling for raw_input (or you could leave them in, but that would overwrite their values from what you passed in the command line) Try running the code you've posted with
$ python ex13.py <yourColor> <yourNumber> <yourShape>
It should work much more closely to what you want.
As you have already solved one problem by removing the variables before the =, now the only problem is you are getting square brackets around ex13.py.
You see you have to add another variable after script before = that is without input() and the problem is solved.
I am new to Python and recently I've made a Mastermind game, and right now I use X and O for checking if you guessed right, but if it possible that I for example use colors for this? I can't find an answer online, I've only found something like
x = Color('#FF0000')
but that doesn't work, it gives me a syntax error
Is this what you're looking for?
Print in terminal with colors using Python?
Basically, you do it by printing ANSI escape sequences.
For example:
print '\x1b[31mThis is sentence is red\x1b[m'