Categories
Python Answers

How to format a string in Python?

Spread the love

Sometimes, we want to format a string in Python.

In this article, we’ll look at how to format a string in Python.

How to format a string in Python?

To format a string in Python, we can use the string’s format method.

For instance, we write:

s = "{0}, {1}, {2}".format(1, 2, 3)
print(s)

We call format with the string placeholders separated by commas.

We create placeholders by using integers surrounded by curly braces.

Therefore, s is '1, 2, 3'.

Conclusion

To format a string in Python, we can use the string’s format method.

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 *