如何在 Markdown 标题中写入 Python 字符串变量?

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

我正在研究 Jupyter 笔记本,我想将 Python 字符串变量的值写入 Markdown 标题。我很清楚 ,在 Markdown 单元格中,

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

这对我有用,但我正在尝试做类似

的事情
# This is a heading with {{a}} in it

但是上次尝试没有成功。我用谷歌搜索了一下,但找不到任何东西。任何帮助将不胜感激。提前致谢!

这是你可以做的事情:

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

我猜你 运行 遇到了问题,因为 '{}' 是 python 字符串中的保留 'word',你可以通过拆分开头和结尾来解决这个问题括号。

这对我有用,但它只是一个代码单元;所以它可能不是您想要的确切功能...

from IPython.display import Markdown as md
a = 'hello'
md("# The data consists of {} observations. Bla, Bla, ....".format(a))