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.