You are currently viewing Pad String with Spaces in Python

How to pad the string with spaces in Python? Adding some characters to the string is known as Padding or filling and is useful when you wanted to make a string with a specific length. In Python, rjust() method can be used to pad spaces at the beginning of the string and ljust() can be used to pad spaces at the end of the string. With the ljust() and rjust() you can fill in any characters but in this article, I will use them to fill in spaces.

Related: Pad String with Zeros

Alternatively, by using f-strings, we can pad spaces to the string in two ways. Using '>', we can pad spaces at left, and using '<', we can pad spaces at right. The same functionality can follow by using format() method.

1. Quick Examples of Padding Spaces to String

If you are in a hurry, below are some quick examples of how to pad or fill spaces to the string to get a specific length of a string.


# Quick Examples of Padding Spaces to String

# Consider the string 
string1 = "PYTHON"

# Example 1: Pad right using rjust()
padded_string = string1.rjust(10)

# Example 2: Pad right with padding character
padded_string = string1.rjust(10, ' ')

# Example 3: Pad left using ljust()
padded_string = string1.ljust(10, ' ')

# Example 4: Pad left using f-strings
padded_string = f"{string1: >10}"

# Example 5: Pad right using f-strings
padded_string = f"{string1: <10}"

# Example 6: Pad left using format()
padded_string = '{: >10}'.format(string1)

# Example 7: Pad right using format()
padded_string = '{: <10}'.format(string1)

2. Pad String with Spaces Using rjust()

The str.rjust() method in Python is used to pad spaces on the left of the string (the string is aligned to the right). This method takes two arguments: the minimum width of the string as a value, and an optional padding character space.

This method returns a new string with the specified width after padding the space character to the original string. For example, If your string length is 6, and the value specified inside rfill() is 10, then 4 spaces are padded to the left of the string, so the final length of the string is 10.

Let’s create a string and pad the spaces at the start (left of the string).


# Consider the string that include 6 characters.
string1 = "PYTHON"
print("Original String:",string1)

# Pad 4 spaces to the left to get string length 10
padded_string = string1.rjust(10)
print("Padded string with spaces:", padded_string)

# Pad 4 spaces by using fill character
padded_string = string1.rjust(10, ' ')
print("Padded string with spaces:", padded_string)

Yields below output.

python pad string spaces

3. Pad Strings with Spaces Using ljust()

The str.ljust() method in Python is used to pad spaces on the right of the Python string (the string is aligned to the left). This method also takes two arguments: the minimum width of the string as a value, and an optional padding character (which is a space by default).

If your string length is 6, and the value specified inside rfill() is 10, then 4 spaces are padded to the right of the string, so the final length of the string is 10. Here, is an example.


# Pad 4 spaces to the right to get string length 10
padded_string = string1.ljust(10, ' ')
print("Padded string with spaces:", padded_string)

# Output:
# Padded string with spaces: PYTHON  

4. Pad Spaces to String Using f-strings

The f-strings in Python are used to display the strings in the desired formats. It can be possible to pad spaces at Left and right with > and < operators.

  1. If we specify element > value, spaces are padded at the start of the string (left padding).
  2. If we specify element < value, spaces are padded at the end of the string (right padding).

The value specified will be equal to the length of the string after padding.

4.1 f-strings Syntax

Let’s look at the syntax of f-strings with left and right paddings.


# Syntax of f-strings
# Specify element padding element as space.

# Syntax of Left padding
f"{string1:element > value}"

# Syntax of Right padding
f"{string1:element < value}"

4.2 Example

Create a string with 6 characters and pad spaces by specifying different values at Left and Right using f-strings.


# Consider the string that include 7 characters.
string1 = "PYTHON"
print("Original String:",string1)

# Pad left using f-strings
padded_string = f"{string1: >10}"
print("Padded string with spaces:", padded_string)

# Pad right using f-strings
padded_string = f"{string1: <10}"
print("Padded string with spaces:", padded_string)

# Output:
# Original String: PYTHON
# Padded string with spaces:     PYTHON
# Padded string with spaces: PYTHON    

Explanation:

  • First example – 4 spaces are Left padded by specifying the value as 10.
  • Second example – 4 spaces are Right padded by specifying the value as 10.
  • In both outputs, the length of the final string is 10, as the value is 10.

5. Pad Spaces to String Using format()

Finally, format() in Python is used to display the strings in the desired formats similar to f-strings. It can be used to pad spaces at Left and Right with > and < operators. But we need to specify this inside {}.

The value specified will be equal to the length of the string after padding.

5.1 format() Syntax

Let’s look at the syntax of format() with Left and Right Paddings.


# Syntax of format()
# Specify element padding element as space.

# Syntax of Left padding
'{:element > value}'.format(string1)

# Syntax of Right padding
'{:element < value}'.format(string1)

5.2 Example

Let’s use the ‘{}’ .format() to pad or fill spaces on the left and right of the string in Python. For example, "{:<{}}" is the format specification within the format() method. The < symbol indicates left alignment, and the {} is a placeholder for the width value specified in the format() method. You can adjust the width variable according to your specific requirements.


# Consider the string that include 6 characters.
string1 = "PYTHON"
print("String:",string1)

# Pad left using format()
padded_string = '{: >10}'.format(string1)
print("Padded string with spaces:", padded_string)

# Pad right using format()
padded_string = '{: <10}'.format(string1)
print("Padded string with spaces:", padded_string)

# Output:
# String: PYTHON
# Padded string with spaces:     PYTHON
# Padded string with spaces: PYTHON   

Frequently Asked Questions on Pad String with Spaces in Python

Why would I need to pad a string with spaces?

Padding strings with spaces is often done to achieve a consistent width or alignment when displaying text, especially in tabular formats or when formatting output. It helps make the output visually more appealing and easier to read.

How do I pad a string on the left side with spaces?

You can use the str.rjust(width, fillchar=' ') method to pad a string on the left side with spaces. Set the width parameter to the desired total width, and the fillchar parameter to the character you want to use for padding (default is space).

Can I pad a string on the right side with spaces?

You can use the str.ljust(width, fillchar=' ') method to pad a string on the right side with spaces. Set the width parameter to the desired total width, and the fillchar parameter to the character you want to use for padding (default is space).

Can I use a character other than a space for padding?

You can specify any character as the fillchar parameter when using the rjust, ljust, or center methods. The default is a space, but you can use any character you like.

What happens if the original string is longer than the specified width?

If the original string is longer than the specified width, the string will not be truncated. No characters will be removed; instead, the original string will be returned unchanged.

How do I center a string with spaces on both sides?

ou can use the str.center(width, fillchar=' ') method to center a string within a specified width. Set the width parameter to the desired total width, and the fillchar parameter to the character you want to use for padding (default is space).

Conclusion

In this article, you have learned rjust() is used to fill or pad the spaces on the left of the string, and ljust() is used to fill on the right of the string in Python. Alternatively, you can also use f-string and format() to fill the spaces on the string to get the desired width of the string.