Ways To Align Text Strings In Python
In this article, I’ll show you various ways to align strings in Python. There are many different ways we can align strings in Python. Let’s have a look at those:
Left Align
For left-aligned text, you can use ljust(…) function, which stands for left justification.
my_text = “Shift me.”
print(my_text.ljust(25) )
The above lines will display left-aligned text with a length of 25.
The alternate way of doing this is by using format(…) function as shown below:
my_text = “Shift me.”
print(format(my_text,’>25'))
Right Align
For right-aligned text, you can use rjust(…) function, which stands for right justification.
my_text = “Shift me.”
print(my_text.rjust(25)
The above lines will display right-aligned text with a length of 25.
The alternate way of doing this is by using format(…) function as shown below:
my_text = “Shift me.”
print(format(my_text,’<25'))