1

I have the following 2d array, displaying as a matrix. I wish to be able to specify the index number of a specific element in the 2d array and turn that number RED (or any colour). Can anyone point me in the right direction please?

Code so far:

>>> grid=[[28, 27, 26, 25,24,23,22], [21,20,19, 18, 17, 16, 15], [8, 9, 10, 11, 12, 13, 14], [7,   6,   5,   4,   3,   2,   1]]
>>> for i in grid:
    print(i)

Output:

[28, 27, 26, 25, 24, 23, 22]
[21, 20, 19, 18, 17, 16, 15]
[8, 9, 10, 11, 12, 13, 14]
[7, 6, 5, 4, 3, 2, 1]

For example:

print(grid[0][2])

Gives the output:

 26

I would like however, grid[0][2] to turn red, when the grid is displayed on the screen again.

To clarify, this is just expected to be output to the console. I am programming in IDLE --> New file. So output to the Python Shell ..I did google and found nothing, so realise this may not be an obvious fix or even a particularly useful thing to do. Still, it's a question .....

Bill Bell
  • 21,021
  • 5
  • 43
  • 58
  • 1
    The colour you get, and how you get it, depends on the medium to which you are outputting something. Not every medium can even display colour. For instance, if you print a number to a black-and-white printer, no matter what you do, the number will appear in either black on white, white on black, black on black or white on white. You need to specify the output medium. That could be a *specific* printing device or class of devices, a web page — or something else. – Bill Bell Feb 01 '17 at 16:58
  • Take a look at http://stackoverflow.com/a/12492996/1072229 – Grisha Levit Feb 01 '17 at 17:02
  • @Bill Bell - thanks. Have edited original question to clarify –  Feb 01 '17 at 17:03
  • @Grisha Levit: ImportError: No module named 'clint' ...that's interesting, but isn't there a more simple method? For instance in VB.Net it is very simple to specify the colour of the text printed to the console. Is there nothing similarly simple in terms of a command in Python? –  Feb 01 '17 at 17:06
  • Python's `print` simply sends a stream of encoded characters to a 'file', which by default is `stdout`, which by default is usually connected to a display device. By default, characters are displayed sequentially in the default font and color. To get other behavior, one must add control code within the character stream; \t skips to the next tab stop, \n to the beginning of the next line. If the 'device' is or simulates a 'dumb terminal', there are the only controls recognized. This is the case with IDLE. It's Shell otherwise displays the characters as received. – Terry Jan Reedy Feb 03 '17 at 03:54
  • To get 'smart terminal' behavior, run your code directly with Python so that print goes to a console that responds to more embedded commands. Modules such as curses (on *nix) and, from 3rd parties on Windows, ncurses and colorama, help with this. – Terry Jan Reedy Feb 03 '17 at 03:59

0 Answers0