Pad String With Spaces in Python

Pad String With Spaces in Python

  1. Use the ljust() Function to Pad the Right End of a String With Spaces in Python
  2. Use the rjust() Function to Pad the Left End of a String With Spaces in Python
  3. Use the center() Function to Pad Both Ends of a String With Spaces in Python
  4. Use String Formatting With the Modulus (%) Operator to Pad a String With Spaces in Python
  5. Use String Formatting With the str.format() Function to Pad a String With Spaces in Python
  6. Use String Formatting With f-strings to Pad a String With Spaces in Python

Padding can be defined as the insertion of non-informative characters that add no value to a given string. It can be done on any single one or both ends of the string.

String padding is useful and comes in handy in string formatting and setting the alignment of the string while printing. This tutorial discusses the different methods available to pad a string with spaces in Python.

Use the ljust() Function to Pad the Right End of a String With Spaces in Python

The ljust() function is utilized to add the right padding to a string, which means padding towards the right end of a string.

The ljust() function contains two parameters: width, and fillchar. The width parameter is mandatory and specifies the length of the given string after the padding process is complete. The fillchar parameter is optional and is used to represent the character we want to pad the string with.

By default, the fillchar value is a space character, that is ' '.

The following code uses the ljust() function to pad the right end of a string with spaces in Python.

a = "I am legend"
print(a.ljust(15))

Output:

I am legend    

Use the rjust() Function to Pad the Left End of a String With Spaces in Python

Having a similar style but different purpose to the ljust() function, the rjust() function is utilized to add the left padding to a string.

Similar to the ljust() function, the rjust() function contains two parameters: width, and fillchar. The width parameter is mandatory and specifies the length of the given string after the padding process is complete. On the other hand, the fillchar parameter is optional and is used to represent the character we want to pad the string with.

This function is very useful when we need to name files that start with numbers, as it helps in achieving a more intuitive sorting practice.

The following code uses the rjust() function to pad the left end of a string with spaces in Python.

a = "I am legend"
print(a.rjust(15," "))

Output:

    I am legend

Use the center() Function to Pad Both Ends of a String With Spaces in Python

The center() function adds padding to both ends of a string in equal amounts. It is utilized to center align a string with the given width of the string.

Much like the ljust() and rjust() methods, the center() function contains two parameters: width and fillchar. Both the functions do exactly the same work, as mentioned in both the methods explained in the article above.

The following code uses the center() function to pad both ends of a string with spaces in Python.

a = "I am legend"
print(a.center(20))

Output:

    I am legend     

Use String Formatting With the Modulus (%) Operator to Pad a String With Spaces in Python

The % sign, also sometimes referred to as the string formatting or interpolation operator, is one way to implement string formatting in Python. Being the oldest of the available ways to implement string formatting, this operator works on almost all versions of Python.

The % sign and a letter representing the conversion type are marked as a placeholder for the variable.

The following code uses string formatting with the modulus (%) operator to pad a string with spaces in Python.

print('%15s' % ('I am legend'))

Output:

    I am legend

In the program above, we do the left padding to the string. We can similarly do the right padding with the following code.

print('%-15s' % ('I am legend'))

Output:

I am legend    

Here, the only drawback is that it can only implement padding on the left end or the right end of a string, but not both ends at once. Moreover, using this method affects the readability of the program.

Use String Formatting With the str.format() Function to Pad a String With Spaces in Python

Another way to implement string formatting in Python is by using the str.format() function. This process makes the curly {} brackets mark the places in the print statement where the variables need to be substituted.

The str.format() was first introduced in Python 2.6 and can be used in all Python versions, from Python 2.6 to Python 3.5. This function is very popular among coders, and the programmers prefer this way to implement string formatting as it is very efficient in handling complex string formatting.

The following code uses string formatting with the str.format() function to pad a string with spaces in Python.

print('{:15}'.format('I am legend'))

Output:

I am legend    

The code above adds the right padding to the given string. To add the left padding to the given string, we can make minor tweaks to the program above and use the following program.

print('{:>15}'.format('I am legend'))

Output:

    I am legend

The str.format() function can also pad the string on both sides at once. Then, all you have to do is add the ^ sign to the placeholder after the colon sign.

The following code uses the str.format() function to pad both ends of a string with spaces in Python.

print('{:^15}'.format('I am legend'))

Output:

  I am legend  

Use String Formatting With f-strings to Pad a String With Spaces in Python

Being introduced with Python 3.6, it is relatively the newest method in Python to implement string formatting. Therefore, this method can be used in more recent and latest versions of Python.

It is more efficient than its other two peers, % sign and str.format(), as it is faster and easier to understand. It also helps in implementing string formatting in Python at a faster rate than the other two methods.

The following code uses string formatting with f-strings to pad the right end of a string with spaces in Python.

width = 15
padding = ' '
print(f'{"I am legend" :{padding}<{width}}')

Output:

I am legend    

To add the left padding to the given string, we can make small tweaks to the code above and use the following program.

width = 15
padding = ' '
print(f'{"I am legend" :{padding}>{width}}')

Output:

    I am legend

Similar to the case of the str.format() function, we can use the ^ sign between the padding and the width specified in the placeholder to pad both the ends of a string with spaces in Python.

width = 15
padding = ' '
print(f'{"I am legend" :{padding}^{width}}')

Output:

  I am legend  

When it comes to using string formatting for achieving the purpose of padding the string, the use of f-strings is recommended the most among the ones we mentioned as it is more concise, readable, and hence, is less prone to errors.

Related Article - Python String

  • Remove Commas From String in Python
  • Check a String Is Empty in a Pythonic Way
  • Convert a String to Variable Name in Python
  • Remove Whitespace From a String in Python
  • Extract Numbers From a String in Python
  • Convert String to Datetime in Python