ljust, rjust and center – string methods in python

In this article, we shall see how to use the rjust(), ljust(), and the center() method in python. They are string methods that can be used to make the string of fixed length by adding padding to the string at the left side or at the right side or both at the left and right sides.

The three methods are,

  1. ljust ()
  2. rjust()
  3. center()

These functions respectively left-justify, right-justify and center a string in a field of given width. They return a string that is at least width characters wide, created by padding the string s with the character fillchar (default is a space) until the given width on the right, left or both sides. The string is never truncated.

1. ljust()

The ljust() method takes two arguments.

  1. The length of the justified string
  2. The filling character

syntax

string.ljust(width, char)

The ljust() method returns the newly justified string. It does not change or alter the original string. We can assign the newly returned string to another variable and access it. Let us look at a simple example for this method.

If no filling char is even then space is used as the default filler. 
If the width is less than or equal to the original string, then the original string will be returned. 
ljust() method in python

The output of the above code is,

pythoncoders------------------
pythoncoders
pythoncoders------------------

As we can see I have used the character ‘‘ as the filler. The ljust() method justifies the content to the left side and fills the remaining width by the filler character.

2. rjust()

The rjust() method is also similar to the ljust() method. But, instead of using filler characters on the right side, this method will justify the content to the right side and use fillers to the left of the string to match the width given in the method.

syntax

string.rjust(width, fill_char)

The rjust() method returns the newly justified string. It does not change the original string. We can assign the newly returned string to another variable and access it. Let us look at a simple example for this method.

If no filling char is even then space is used as the default filler. 
If the width is less than or equal to the original string, then the original string will be returned. 
rjust() method in python

The output of the above code is,

------------------pythoncoders
pythoncoders
------------------pythoncoders
                  pythoncoders

As we can see I have used the character ‘‘ as the filler. The rjust() method justifies the content to the right side and fills the remaining width by the filler character.

3. center

The center method justifies the string to the center and fills both the left and right side of the string with the filling characters to match the width.

syntax

string.center(width, fill_char)

The center() method returns the newly justified string. It does not change the original string. We can assign the newly returned string to another variable and access it. Let us look at a simple example for this method.

If no filling char is even then space is used as the default filler. 
If the width is less than or equal to the original string, then the original string will be returned. 
center() method in python

The output of the above code is,

---------pythoncoders---------
pythoncoders
---------pythoncoders---------
         pythoncoders         

As we can see I have used the ‘‘ as the filler. The center() method justifies the content to the center and fills the remaining width by the filler character.

Conclusion

String alignment is frequently used in many day-day applications. Python in its language offers several functions that helps to align string. Also, offers a way to add user specified padding instead of blank space.

Hope this article is helpful. If you have any queries feel free to ask them in the comments.

Happy coding!