Categories
Python Answers

How to format output string with right alignment with Python?

Spread the love

Sometimes, we want to format output string with right alignment with Python.

In this article, we’ll look at how to format output string with right alignment with Python.

How to format output string with right alignment with Python?

To format output string with right alignment with Python, we can use the string format method.

For instance, we write

line_new = '{:>12}  {:>12}  {:>12}'.format(word[0], word[1], word[2])

to call '{:>12} {:>12} {:>12}'.format with the 3 items to put in place of the placeholders.

The placeholders specify that the width of each column is 12 characters and that the alignment of each item is right.

Conclusion

To format output string with right alignment with Python, we can use the string 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 *