Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write a Python string variable in a Markdown heading? [duplicate]

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!

like image 540
Ignacio Correa Avatar asked Sep 14 '26 09:09

Ignacio Correa


2 Answers

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))
like image 159
Mason Caiby Avatar answered Sep 15 '26 23:09

Mason Caiby


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.

like image 23
PhoenixFlame101 Avatar answered Sep 15 '26 23:09

PhoenixFlame101



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!