Вы можете просто использовать f-strings
следующим образом:
hello = "Hey"
world = "Earth"
test = f"""{hello} {world}"""
print(test)
вывод:
Hey Earth
Просто добавьте f
перед строкой, и используйте фигурные скобки ({python-code}
) для вставки кода Python в строку (в приведенном выше примере две переменные с именами hello
и world
соответственно).
Используя ваш код в качестве примера:
from markdown import markdown
fish = "salmon"
s = f"""
| Type | Value |
| ------------- | -------------|
| Fish | {fish} |
"""
html = markdown(s, extensions=["tables"])
print(html)
Выходы:
<table>
<thead>
<tr>
<th>Type</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Fish</td>
<td>salmon</td>
</tr>
</tbody>
</table>
Если вы используете Python 2.7, вы можете используйте функцию str.format()
:
from markdown import markdown
fish = "salmon"
s = """
| Type | Value |
| ------------- | -------------|
| Fish | {} |
""".format(fish)
html = markdown(s, extensions=["tables"])
print(html)