How do I use text.format() when the text has curly braces from HTML CSS?

I have e.g. this code:

helpAbout = """
<!doctype html>
<style>
    td, th {padding: 0px 30px 0px 0px;}
</style>
<tr><td>Author:</td> <td>{}</td></tr>
"""
print(helpAbout.format(__author__))

which fails, because Python tries to insert __author__ where the curly braces from the CSS style command are.

I help myself by using the outdated ā€˜%ā€™ formatting option.

helpAbout = """
<!doctype html>
<style>
    td, th  { padding: 0px 30px 0px 0px;}
</style>
<tr><td>Author:</td> <td>%s</td></tr>
"""
print(helpAbout % __author__)

This works. But Iā€™d prefer to use the str.format() option consistently. Am i missing something or is it really true that str.format() has this shortcoming?

To insert literal curlies in a format string, double them:

<style>
    td, th {{padding: 0px 30px 0px 0px;}}
</style>
3 Likes

Oh, that easy. Many thanks!