Python String rjust() and ljust() With Examples

Python string API provides two utility function to create a new string of specified length from the source string with right and left justification.

Python String rjust()

This function returns a new string of specified length with right-justified source string. We can specify the character to use for the padding, the default is whitespace. If the specified length is smaller than the source string, then the source string is returned.

Let’s look at some examples of rjust() function.


If you are not familiar with f-prefixed string formatting, please read f-strings in Python.

Python String ljust()

Python string ljust() is very similar to the rjust() function. The only difference is that the original string is right-justified. Let’s have a look at some examples.


If you want center-aligned string then you can use Python String center() function.

Error Scenarios with rjust() and ljust() functions

Let’s see some error scenarios that may come when using rjust() and ljust() functions.

Error: TypeError: ‘str’ object cannot be interpreted as an integer

Python String rjust() and ljust() With Examples

Official Documentation: rjust(), ljust()

By admin

Leave a Reply