How to format a string in Python?

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.