Fill a Python String with Spaces
Last Updated :
13 Feb, 2023
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:
- For filling space in right: '{: <space}'.format(string)
- For filling space in left: '{: >space}'.format(string)
- 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)
Similar Reads
Read File As String in Python
Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File
3 min read
replace() in Python to replace a substring
replace() method in Python allows us to replace a specific part of a string with a new value. It returns a new string with the change without modifying the original one. We can also specify how many times the replacement should occur. For Example: [GFGTABS] Python s = "hlo AB world" # Repl
2 min read
Convert String to Set in Python
There are multiple ways of converting a String to a Set in python, here are some of the methods. Using set()The easiest way of converting a string to a set is by using the set() function. Example 1 : [GFGTABS] Python s = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s
1 min read
Convert Set to String in Python
Converting a set to a string in Python means changing a group of unique items into a text format that can be easily read and used. Since sets do not have a fixed order, the output may look different each time. For example, a set {1, 2, 3} can be turned into the string "{1, 2, 3}" or into "{3, 1, 2}"
3 min read
Working with Strings in Python 3
In Python, sequences of characters are referred to as Strings. It used in Python to record text information, such as names. Python strings are "immutable" which means they cannot be changed after they are created. Creating a StringStrings can be created using single quotes, double quotes, or even tr
6 min read
Python - Separate first word from String
We need to write a Python program to split a given string into two parts at the KáµÊ° occurrence of a specified character. If the character occurs fewer than K times, return the entire string as the first part and an empty string as the second part. Separating the first word from a string involves ide
2 min read
string.whitespace in Python
In Python, string.whitespace is a string containing all the characters that are considered whitespace. Whitespace characters include spaces, tabs, newlines and other characters that create space in text. string.whitespace constant is part of the string module, which provides a collection of string o
3 min read
How to Index and Slice Strings in Python?
In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence. Table of Content Indexing Strings in PythonAccessin
2 min read
f-strings in Python
Python offers a powerful feature called f-strings (formatted string literals) to simplify string formatting and interpolation. f-strings is introduced in Python 3.6 it provides a concise and intuitive way to embed expressions and variables directly into strings. The idea behind f-strings is to make
5 min read
Python String split()
Python String split() method splits a string into a list of strings after breaking the given string by the specified separator. Example: [GFGTABS] Python string = "one,two,three" words = string.split(',') print(words) [/GFGTABS]Output: ['one', 'two', 'three']Python String split() M
6 min read