How to Print a Fixed-Width String in Python

In this tutorial , we will learn 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.

The format() method in Python is a string method that formats the specified value(s) and insert them inside the string's placeholder. The placeholder is defined using curly braces: {}. Read more about the placeholders in the Placeholder section below. The format() method returns the formatted string.

Code Example: 

'{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.


1.40 GEEK