How to Fill out a String with spaces in Python?

In this python tutorial, we will learn to fill out a string with spaces.

Table Of Contents

Fill out a string with spaces using rjust()

In this method, we can fill the spaces in the existing string from the left and make the string right justified. It can be possible to fill any number of spaces. The function rjust() takes two parameters. The first parameter is the total characters in a string after filling the space and the second parameter takes a space i.e. ” “.

Parameters:

  1. the First parameter represents the integer which refers to the total number of characters in a string after filling with spaces.
  2. the Second parameter takes an empty string that fills the string from the left.

It returns a string right-justified and padded with spaces in the left. After padding of spaces in left of string, the given length of string should be equal to first parameter.

For example, if the string length is 12, if we specify 20 as the first parameter, then 8 spaces are allocated before the 12 characters. So 12+8 =20.

Example:

In this example, we will consider a string – “Welcome to thisPointer” and fill the string with

  1. 8 spaces
  2. 2 spaces and
  3. 20 spaces
# String
string1 = "Welcome to thisPointer"

print(string1)

# Fill the string with 8 spaces
print("Filling the string with 8 spaces - ",string1.rjust(30," "))

# Fill the string with 2 spaces
print("Filling the string with 2 spaces - ",string1.rjust(24," "))

# Fill the string with 20 spaces
print("Filling the string with 20 spaces - ",string1.rjust(42," "))

Output:

Welcome to thisPointer
Filling the string with 8 spaces -          Welcome to thisPointer
Filling the string with 2 spaces -    Welcome to thisPointer
Filling the string with 2 spaces -                      Welcome to thisPointer

Here, the Length of the actual string is 22. So for padding this string with,

  1. 8 spaces, we need to specify 22+8 =30 as the first parameter. Hence the final string is of length 30.
  2. 2 spaces, we need to specify 22+2 =24 as the first parameter. Hence the final string is of length 24.
  3. 20 spaces, we need to specify 22+20 = 42 as the first parameter. Hence the final string is of length 42.

Fill out a string with spaces using ljust()

In this method, We can fill the spaces in the existing string from the right to make the string left justified. It can be possible to fill any number of spaces. ljust() takes two parameters. the first parameter is the total characters in a string after filling the space and the second parameter takes a space – ” “.

Parameters:

  1. The First parameter represents the integer which refers to the total number of characters in a string after filling with spaces.
  2. The Second parameter takes an empty string that fills the string from the right.

For example, if the string length is 12, if we specify 20 as the first parameter, then 8 spaces are allocated after 12 charcaters. So 8+12 =20.

Example:

In this example, we will consider a string – “Welcome to thisPointer” and fill the string with

  1. 8 spaces
  2. 2 spaces and
  3. 20 spaces
# String
string1="Welcome to thisPointer"

print(string1)

# Fill the string with 8 spaces
print("Filling the string with 8 spaces from left- \"", string1.ljust(30," ") , "\"")

# Fill the string with 2 spaces
print("Filling the string with 2 spaces from left- \"", string1.ljust(24," ") , "\"")

# Fill the string with 20 spaces
print("Filling the string with 20 spaces from left- \"", string1.ljust(42," ") , "\"")

Output:

Welcome to thisPointer
Filling the string with 8 spaces from left- " Welcome to thisPointer         "
Filling the string with 2 spaces from left- " Welcome to thisPointer   "
Filling the string with 20 spaces from left- " Welcome to thisPointer                     "

Here, the Length of the actual string is 22. So for padding this string with,

  1. 8 spaces, we need to specify 22+8 =30 as the first parameter. Hence the final string is of length 30.
  2. 2 spaces, we need to specify 22+2 =24 as the first parameter. Hence the final string is of length 24.
  3. 20 spaces, we need to specify 22+20 = 42 as the first parameter. Hence the final string is of length 42.

Fill out a string with spaces using center()

In this method, We can fill the spaces in the existing string. It can be possible to fill any number of spaces. The center() fills the string with spaces from both the sides i.e. left and right of the string. It takes two parameters. the first parameter is the total characters in a string after filling the space and the second parameter takes a space i.e. ” “.

Parameters:

  1. The First parameter represents the integer which refers to the total number of characters in a string after filling with spaces
  2. The Second parameter takes an empty string that fills the string from the both the sides.

For example, if the string length is 12, if we specify 20 as the first parameter, then 4 spaces are allocated to left from center and 4 spaces are allocated to right from center. so 4+12+4 =20.

Example:

In this example, we will consider a string – “Welcome to thisPointer” and fill the string with
1. 8 spaces
2. 2 spaces and
3. 20 spaces

# String
string1="Welcome to thisPointer"

print(string1)

# Fill the string with 8 spaces
print("Filling the string with 8 spaces from left- \"", string1.center(30," ") , "\"")

# Fill the string with 2 spaces
print("Filling the string with 2 spaces from left- \"", string1.center(24," ") , "\"")

# Fill the string with 20 spaces
print("Filling the string with 20 spaces from left- \"", string1.center(42," ") , "\"")

Output:

Welcome to thisPointer
Filling the string with 8 spaces from left- "     Welcome to thisPointer     "
Filling the string with 2 spaces from left- "  Welcome to thisPointer  "
Filling the string with 20 spaces from left- "           Welcome to thisPointer           "

Here, the Length of the actual string is 22. So for padding this string with

  1. 8 spaces, we need to specify 4+22+4 =30 as the first parameter. Hence the final string is of length 30.
  2. 2 spaces, we need to specify 1+22+1 =24 as the first parameter. Hence the final string is of length 24.
  3. 20 spaces, we need to specify 10+22+10 =42 as the first parameter. Hence the final string is of length 42.

Fill out a string with spaces using the % operator

It fills the string with a space from the starting position of the string.

Syntax:

'%ns' % (string)

Where string is the input string and n represent the length of the string after spaces.

For example, String length is 22, and if we specify the value as 23, then it fills the first position in the string as ” ” and returns the entire string from the second position of the string.

For Example:

# String
string1="Welcome to thisPointer"

print(string1)

# Fill the string with 1 space
print('"', '%23s' % (string1), '"')

# Fill the string with 5 spaces
print('"', '%27s' % (string1), '"')

Output:

Welcome to thisPointer
"  Welcome to thisPointer "
"      Welcome to thisPointer "

We filled the string with 1 space and 5 spaces.

Summary

We have seen four different ways to fill out the string with spaces. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top