Andrew
Andrew

Reputation: 1126

How to print italic text in f-strings in Jupyter Notebook

I am trying to use f-strings to print some parts of text in italic format.

Browsing the web for "python"+"print"+"italic"+"f-strings", I found these ANSI codes should do the work : '\x1B[3m' and '\x1B[0m' ; although they don't :

print(f'\x1B[3m italic \x1B[0m' + f'not italic') 

gives no italic output :

italic not italic

Am I missing some update? My python version is 3.9.7 and I'm using Jupyter Notebook.

Upvotes: 1

Views: 2335

Answers (2)

ovhaag
ovhaag

Reputation: 1278

In Jupyter Notebooks, you can use

from IPython.display import Markdown, display
display(Markdown("normal *italic* normal"))

See printing bold, colored, etc., text in ipython qtconsole, https://stackoverflow.com/a/46934204/747202

Upvotes: 1

ljmc
ljmc

Reputation: 5264

ANSI escape work with python or ipython, but only in the terminal.

terminal screenshot

Upvotes: 0

Related Questions