Color method for str object


\033[91mHello, World!\033[0m // in bash would appear green

the method wraps the string with ansi escape code and returns it colored for display in terminal window.

1 Like

what do u all think?

Moving this to the Help category, as this is not sufficient for an Ideas discussion. Do not move it back.

There are plenty of libraries that provide color and other terminal features. For example, Rich, Textual, Click, Blessings, Colorama, etc.

yeah but it would be great if the import of an external library wouldnt be necessary. it would be easier and quicker.

it could be done even an h1 wrapper or any html tag. doing natively would save a lot of imports

That’s not a sufficient reason to add something to the standard library, as it’s true for literally everything imaginable. Installing packages and importing them is ok, it’s a standard part of Python.

1 Like

i think is pretty standard to color a string in a print function. have u ever colored a string that u printed?

I have never thought to do this because it has no relevance to the kind of python I write.

Even if something like this was needed in the stdlib, I don’t think a method on str would be the best place for it. It seems extremely specific - for example, I don’t think Windows-based terminals tend to support ANSI escape codes - and it’s not particularly obvious (at least to me) what it would do based on the name alone.

Yes, frequently, but it’s still not appropriate as a method on the string. You could just as easily do it this way:


By the way, if you want a proposal to be taken seriously, I strongly recommend that you test your example code. A method on the string "hello" should not return the string "Hello, World!" and 91m does not create green. It makes it more difficult to come up with useful alternate suggestions, since I don’t know whether you intended to have this be foreground, background, bright, non-bright, red, green, or anything else.

This works in the windows terminal app. It was one of the design goals.
But not the older cmd.exe world. (I forget the api names these are built on).

And that is exactly why this would not be added to the standard library. There are countless ways the text could be modified to indicate the desired formatting, and the correct one will depend on the context. There are many terminals, for example, that don’t support ANSI, for which modifying the string in the originally proposed way would just make it print garbage. (For that matter, imagine if a user asked for “green” text this way, and then tried to insert it into a larger HTML document?)

Not everything needs to be in the stdlib. We routinely import many
external libraries for almost any nontrivial code.

Your proposal seems to suggest native support for embedding ANSI colour
escape sequences in a string. That’s not the only way things are

As others have mentioned, there are any number of existing libraries
which help provide ANSI colour (or more generic stuff - not everything
supports ANSI colour, even things which support colour).

Cameron Simpson

I will just note that when using a tkinter (tk) text widget, there is yet another method for coloring text. Same for other GUIs and TUIs (Text UI) and graphics systems.

Thank’s i’m avoiding too the use of external library for thing python can do natively. But it’s more char in in the console, not realy faster didn’t do that in a loop like % of completion

here a list for mani color background bold and italic:

list= [0, 1, 3, 7, 9, 21, 51, 52, 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97, 41, 42, 43, 44, 45, 46, 47, 100, 101, 102, 103, 104, 105, 106, 107]
[print(f"{a} \033[{a}mHello, World!\033[0m") for a in list]

1 Like

Thank’s i’m avoiding too the use of external library for thing python
can do natively.

Care to elaborate on why?

The stdlib isn’t so much “native” to Python as “shipped with”. Of
course, I’m inventing a meaning here for “native”, which my intuition
associates with features of the interpeter itself eg ints, functions

But it’s more char in in the console, not realy faster didn’t do that
in a loop like % of completion

I don’t understand this sentence.

i m just saying it"s more caracteres to print even if we don’t see.

1 Like