Open In App

Fill a Python String with Spaces

Improve
Improve
Like Article
Like
Save
Share
Report

This article will show you to fill out a python string with spaces to the left, right, and around a string in Python.

Let’s suppose we have a string GeeksforGeeks, and we want to fill N number of spaces on left, right, and around the string.

Note that in the case of space around the string if N is odd then the extra space is added to the right of the string.

Example:

Input:
string = "GeeksforGeeks"
N = 2
Output:
Right: "GeeksforGeeks  "
Left: "  GeeksforGeeks"
Around: " GeeksforGeeks "

Input:
string = "Geek"
N = 5
Output:
Right: "Geek     "
Left: "     Geek"
Around: "  Geek   "

Using the format() method to fill out a Python string with spaces

For more effective handling of sophisticated string formatting, the Python string format() method has been developed. Instead of writing a print statement every time we utilize the idea of formatting, there are instances when we wish to write generalized print statements.

Using the format method you can give spaces to left, right and Both left and right i.e., the center of the string as follows:

  1. For filling space in right: ‘{: <space}’.format(string)
  2. For filling space in left: ‘{: >space}’.format(string)
  3. For filling space in Both left and right: ‘{: ^space}’.format(string)

Where, space is the space given to the left, right, or around the string + length of the string.

This is demonstrated in the following example:

Python3




# original string
string = "GFG"
 
# Right Padding of the string
right_padding = ('{: <5}'.format(string))
print(f'\"{right_padding}\"')
 
# Left Padding of the string
left_padding = ('{: >5}'.format(string))
print(f'\"{left_padding}\"')
 
# Center Padding of the string
central_padding = ('{: ^5}'.format(string))
print(f'\"{central_padding}\"')


Output:

"GFG  "
"  GFG"
" GFG "

If you want any other character to fill out then for this then just replace the space with the intended character. This is shown as follows:

Python3




# original string
string = "GFG"
 
# Right Padding of the string
right_padding = ('{:$<5}'.format(string))
print(f'\"{right_padding}\"')
 
# Left Padding of the string
left_padding = ('{:$>5}'.format(string))
print(f'\"{left_padding}\"')
 
# Center Padding of the string
central_padding = ('{:$^5}'.format(string))
print(f'\"{central_padding}\"')


Output:

"GFG$$"
"$$GFG"
"$GFG$"

Time complexity: O(n)

Space complexity: O(n)

Using the ljust(), rjust(), and center()  methods to fill out a Python string with spaces

This approach employs the ljust(), rjust(), and center()  methods to achieve the intended goal.

  • ljust(): It is used to fill out the spaces/characters to the right of the given string.
  • rjust(): It is used to fill out the spaces/characters to the left of the given string.
  • center(): It is used to fill out the spaces/characters to both left and right of the given string.

The following demonstrates the appropriate syntax for the above methods:

  • For filling the space in right: string.ljust(space)
  • For filling the space on the left: string.rjust(space)
  • For filling space in Both left and right: string.center(space)

where, space is the space given to the left, right or center padding + length of the string.

The following example implements the above approach:

Python3




# original string
string = "GFG"
 
# Right Padding of the string
right_padding = string.ljust(5)
print(f'\"{right_padding}\"')
 
# Left Padding of the string
left_padding = string.rjust(5)
print(f'\"{left_padding}\"')
 
# Center Padding of the string
central_padding = string.center(5)
print(f'\"{central_padding}\"')


Output:

"GFG  "
"  GFG"
" GFG "

Time complexity: O(n)

Space complexity: O(n)

If you want any other character to fill out, try the following:

Python3




# original string
string = "GFG"
 
# Right Padding of the string
right_padding = string.ljust(5, '$')
print(f'\"{right_padding}\"')
 
# Left Padding of the string
left_padding = string.rjust(5, '$')
print(f'\"{left_padding}\"')
 
# Center Padding of the string
central_padding = string.center(5, '$')
print(f'\"{central_padding}\"')


Output:

"GFG$$"
"$$GFG"
"$GFG$"

Time complexity: O(n)

Space complexity: O(n)



Last Updated : 13 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads