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

Following are quick examples of how to pad or fill spaces to the string to get a specific length of a string.


# Consider the string.
string1 = "PYTHON"
print("String: ",string1)

# Pad right using rjust()
print(string1.rjust(10))

# Pad right with padding character
print(string1.rjust(10, ' '))

# Pad left using ljust()
print(string1.ljust(10, ' '))

# Pad left using f-strings
print(f"{string1: >10}")

# Pad right using f-strings
print(f"{string1:10}".format(string1))

# Pad right using format()
print('{:''<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("String: ",string1)

# Pad 4 spaces to the left to get string length 10
print(string1.rjust(10))

# Pad 4 spaces by using fill character
print(string1.rjust(10, ' '))

#Output:
#String:  PYTHON
#    PYTHON
#    PYTHON

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
print(string1.ljust(10, ' '))

# Output:
# 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.

5.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}"

5.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("String: ",string1)

# Pad left
print(f"{string1: >10}")

# Pad right
print(f"{string1: <10}")

# Output:
# String:  PYTHON
#     PYTHON
# 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.

6. 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.

6.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)

6.2 Example

Let’s use the ‘{}’ .format() to pad or fill spaces on the left and right of the string in python.


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

# Pad left
print('{: >10}'.format(string1))

# Pad right
print('{: <10}'.format(string1))

# Output:
# String: PYTHON
#     PYTHON
# PYTHON    

7. 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 stirng.

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply