python - 在 Jupyter 中以颜色打印 f 字符串,并在混合中使用转义字符
问题描述
我知道以前有人问过类似的问题,但我找不到任何确切的问题。假设我有这个列表:
tags = ['<div>','<body>','<h1>']
我可以在这里轻松地使用 f 字符串:
for tag in tags:
print(f'this is your tag: {tag}')
输出:
this is your tag: <div>
this is your tag: <body>
this is your tag: <h1>
到现在为止还挺好。但我真正想做的是获得相同的输出,但标签名称打印为红色,例如红色。这就是我遇到括号问题的地方。如果我使用:
from IPython.display import HTML as html_print
for tag in tags:
html_print(f'this is your tag: {tag}')
什么也没有打印出来——即使我删除了标签。
我试过:
from IPython.display import Markdown, display
然后首先:
for tag in tags:
display(f'this is your tag: {tag}')
这就像一个普通的print
.
但是,如果我尝试:
for tag in tags:
display(Markdown((f'this is your tag: {tag}')))
输出是:
this is your tag:
this is your tag:
this is your tag:
我的理解是我需要Markdown
用彩色打印,但显然括号会导致 f 字符串出现问题 in Markdown
,这与使用print
and的情况不同display
。那么我该如何解决呢?
解决方案
推荐阅读
- python-3.x - 基于输入,如何将列表的元素分类为类的对象?
- wireshark - 如何使用 Wireshark 从 pcap 文件中提取所有特征?
- tensorflow - 如何批量预测 Faster RCNN 模型?
- flask - 两个模型之间的SQL db关系
- android - React Native 运行 - Android 产生奇怪的错误
- r - 捕获组的正则表达式
- janusgraph - janusGraph 服务器实例是怎么来的?
- linux - 如何在linux中安装fly
- amazon-web-services - 切换配置文件 AWS Secret Manager - .Net Core
- python - 在 Windows 上使用默认版本的 python?