2

I'm working on Jupyter Notebooks and I'd like to write the value of a Python string variable into a Markdown heading. I'm well aware that one can write, in a Markdown cell,

# Python
a = 'hello'
In a markdown cell, a is {{a}}

and this works for me, but I'm trying to do something like

# This is a heading with {{a}} in it

But the last attempt didn't work. I googled a little bit, but I couldn't find anything. Any help will be appreciated. Thanks in advance!

Ignacio Correa
  • 147
  • 1
  • 6

2 Answers2

4

This works for me, but it is just a code cell; so it might not be the exact functionality you want...

from IPython.display import Markdown as md
a = 'hello'
md("# The data consists of {} observations. Bla, Bla, ....".format(a))
Mason Caiby
  • 1,846
  • 6
  • 17
1

This is something you can do:

"This is a string with {{" + a + "}} in it"

I guess you're running into issues because '{}' is a reserved 'word' in python strings, and you can solve this issue by splitting the opening and closing brackets.