How to print literal curly-brace characters in a string and also use .format it with Python?

Sometimes, we want to print literal curly-brace characters in a string and also use .format it with Python.

In this article, we’ll look at how to print literal curly-brace characters in a string and also use .format it with Python.

How to print literal curly-brace characters in a string and also use .format it with Python?

To print literal curly-brace characters in a string and also use .format it with Python, we can use double curly braces in the string.

For instance, we write:

x = " {{ Hello }} {0} "
print(x.format('jane'))

to surround ‘Hello’ with double curly braces, which will be return a string with single curly braces surround it.

Therefore, we get ' { Hello } jane ' returned and printed.

Conclusion

To print literal curly-brace characters in a string and also use .format it with Python, we can use double curly braces in the string.