How to Justify Text in Python



Python


In this article, we show how to justify text in Python.

Justifying text is aligning the text left, right or center, depending on your preferences or needs for a certain application.

Left justifying text is having the text aligned to the left.

Right justifying text is having the text aligned to the right.

Center justifying text is having the text centered.

So let's see how to do this now in Python and how the output looks.

This is shown in the code below.



So to left justify text, we use the ljust() function. This function takes 2 parameters. The first is how many characters in total are in the string. So, realize that this is not how many characters there are of the character you chose to use during the justifying. It is the total number of characters in the string in total. So if you are displaying text in which the number of characters in the text is greater than the number specified within the function, the character you chose will not be shown. The second parameter is the

To right justify text, we use the rjust() function.

To center justify text, we use the center() function.

The special character that you choose can be anything, including whitespace.

In fact, below we show the same code above but with whitespace, which may be what you would want.



So this is the same code as before, but with whitespace now as the chosen character.

And this is how we can justify text either left, right, or center in Python.


Related Resources

How to Draw a Rectangle in Python using OpenCV

How to Draw a Circle in Python using OpenCV

How to Draw a Line in Python using OpenCV

How to Add Text to an Image in Python using OpenCV

How to Display an OpenCV image in Python with Matplotlib

How to Use Callback functions to Connect Images to Events in Python using OpenCV

How to Check for Multiple Events in Python using OpenCV



HTML Comment Box is loading comments...