Categories
Python Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *