Categories
Python Answers

How to print a string at a fixed width with Python?

Spread the love

Sometimes, we want to print a string at a fixed width with Python.

In this article, we’ll look at how to print a string at a fixed width with Python.

How to print a string at a fixed width with Python?

To print a string at a fixed width with Python, we can use the string format method.

For instance, we write

'{0: <5}'.format('s')

to call ‘{0: <5}’.formatwith‘s’` to print ‘s’ in a space 5 characters long and left aligned.

We use < to left align the string and we specify the length after that.

Conclusion

To print a string at a fixed width 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 *