Python: String Padding

There are 2 main ways we can add padding to a string in Python-

Using string formatting functions.
Using f-strings.

Method #1: String Padding Functions

Use the string functions like “ljust()“, “rjust()“, “center()“, “zfill()” to get the desired width by adding some padding to the string.

str.ljust() - left justify original

Use the “ljust()” function to left justify the original string. Here is the signature of “ljust()” function-

str.ljust(self, __width: int, __fillchar: str = ...) -> str
Python
Provide the desired length of the string after padding. Here we have set the length as 20.
You can fill character that will be added to fill the string to the desired length.
If the fill character is not provided, then space will be used.
name = "BigBoxCode"

result = name.ljust(20)

print(f"-{result}-")

result = name.ljust(20, "*")

print(f"-{result}-")
Python

Output:

Our original string(“BigBoxCode”) length is 10 here.
In the first output, 10 spaces are added to fill the original string, as we have not set the fill character.
In the second output- 10 asterisks(*) are added to fill the original string and make it of length 20.
-BigBoxCode          -

-BigBoxCode**********-
Plaintext
The fill character can be only 1 character. We can not use multiple characters as fill character.
If multiple characters are used as fill character, then it throws a “TypeError”.
name = "BigBoxCode"

result = name.ljust(20, "*9")

print(f"-{result}-")
Python

Output:

Traceback (most recent call last):
  File "bigboxcode.py", line 3, in <module>
    result = name.ljust(20, "*9")
TypeError: The fill character must be exactly one character long
Plaintext

str.rjust() - right justify original

We can use “rjust()” to right justify the original string. Here is the signature of “rjust()” function-

str.rjust(self, __width: int, __fillchar: str = ...) -> str
Python
Here we have used “rjust()” to right justify our original string.
We have set the desired length to 20.
We have used asterisk(*) as the fill character. If no fill character is used then space will be used.
name = "BigBoxCode"

result = name.rjust(20, "*")

print(f"-{result}-")
Python

Output:

-**********BigBoxCode-
Plaintext

str.center()

Use “center()” function to center justify the original string. Here is the signature-

str.center(self, __width: int, __fillchar: str = ...) -> str
Python
We have used “center()” function to middle justify the original string.
Our desired length of the string is 20.
We have used asterisk(*) as fill character.
name = "BigBoxCode"

result = name.center(20, "*")

print(f"-{result}-")
Python

Output:

5 fill characters are added on the left and 5 on the right.

-*****BigBoxCode*****-
Plaintext
Use 21 as the desired with of the string.
name = "BigBoxCode"

result = name.center(21, "*")

print(f"-{result}-")
Python

Output:

6 fill characters are added on the left and 5 on the right.

-******BigBoxCode*****-
Plaintext

str.zfill() - fill with zero

Use “zfill()” to fill the string with zeros(0), to achieve the desired width. Here is the signature of zfill-

str.zfill(self, __width: int) -> str
Python
We have used “zfill()“, and our desired width is 4.
num = "23"
result = num.zfill(4)
print(result)
Python

Output:

Our original string is “23” and 2 zeros are added in the front to make it of width of 4.

0023
Plaintext

Method #2: f – String

We can set the string padding, while using a string formatting.

< - left align original string

Use less than(<) sign to left justify a string in the final padded string.
Here we have set our desired width to be 20.
We can define the fill character before the less than(<) sign. Here we have used asterisk(*) as the fill character. If no fill character is used, then it will use space.
name = "BigBoxCode"

print(f"-{name:<20}-")

print(f"-{name:*<20}-")
Python

Output:

-BigBoxCode          -

-BigBoxCode**********-
Plaintext

> - right align original string

Use greater than(>) sign to right justify a string.
Here we have set our desired width to be 20.
Here we have used asterisk(*) as the fill character. If no fill character is used, then it will use space.
name = "BigBoxCode"

print(f"-{name:>20}-")

print(f"-{name:*>20}-")
Python

Output:

-          BigBoxCode-

-**********BigBoxCode-
Plaintext

^ - center align original string

Use tilda(^) sign to center justify a string.
Here we have set our desired width to be 20.
Here we have used asterisk(*) as the fill character. If no fill character is used, then it will use space.
name = "BigBoxCode"

print(f"-{name:^20}-")

print(f"-{name:*^20}-")

print(f"-{name:*^21}-")
Python

Output:

If an odd number is used as width, then the right part has 1 more fill character than the right.

Here in the last example, we have used 21 as desired width. So, 5 asterisks(*) are used on the left and 6 on the right.

-     BigBoxCode     -

-*****BigBoxCode*****-

-*****BigBoxCode******-
Plaintext

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.