Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: make Rich not style a string

Tags:

python

rich

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?

like image 911
Longino Avatar asked Oct 15 '25 11:10

Longino


1 Answers

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.

like image 198
Will McGugan Avatar answered Oct 18 '25 06:10

Will McGugan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!