Search code examples
pythonrich

Python: make Rich not style a string


I'm using rich to style the outputs in the terminal. I need to style random generated text that sometimes have within itself characters that make Rich style it ("[words]", "[/]") in another way.

Basically I code rich to make a string appear green but since the presence of this characters the string appears with small random colored part.

How can I tell to rich to style a string how I want to and stop there, instead to also look for the characters inside it?


Solution

  • You can escape a string, so Rich won't style things that look like markup tags. Easiest way to do this would be to import escape from rich.markup.

    from rich import print
    from rich.markup import escape
    print(escape("this is [literal]"))
    

    See the docs on escaping console markup.