0

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.

ljmc
  • 4,830
  • 2
  • 7
  • 26
Andrew
  • 926
  • 2
  • 17
  • 24
  • 1
    ANSI escape codes only work on the command line (in a terminal). They don't work in graphical applications. – MattDMo Aug 15 '22 at 14:57
  • But I used `print(f'\033[1m CITY \033[0m')` and it works and gives me **CITY** – Andrew Aug 15 '22 at 15:02

2 Answers2

0

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

terminal screenshot

ljmc
  • 4,830
  • 2
  • 7
  • 26
  • Well, so I don't understand why it works in my Jupyter Notebook if I ask for `bold` text but not if I ask for `italic` text...? Very strange. – Andrew Aug 15 '22 at 18:14
  • They might have implemented a subset of all ANSI sequences. – ljmc Aug 15 '22 at 18:26
0

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

ovhaag
  • 1,168
  • 2
  • 9
  • 16