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
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:Pythons = "hlo AB world" # Replace "AB" with "C" in `s
1 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 tri
5 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
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
String rjust() and ljust() in python() 1. String rjust() The string rjust() method returns a new string of given length after substituting a given character in left side of original string. Syntax: string.rjust(length, fillchar) Parameters: length: length of the modified string. If length is less than or equal to the length of the origin
2 min read
What does %s mean in a Python format string? In Python, the %s format specifier is used to represent a placeholder for a string in a string formatting operation. It allows us to insert values dynamically into a string, making our code more flexible and readable. This placeholder is part of Python's older string formatting method, using the % o
3 min read
Convert string to a list in Python Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
2 min read
String Alignment in Python f-string String alignment in Python helps make text look neat and organized, especially when printing data of different lengths. Without formatting, output can appear messy and hard to read. Pythonâs f-strings make it easy to align text by controlling its position within a set space. Pythonâs f-strings allow
2 min read
Python Raw Strings In Python, a raw string is a special type of string that allows you to include backslashes (\) without interpreting them as escape sequences. In this article, we will see how to take care of a backslash combined with certain alphabet forms literal characters which can change the entire meaning of th
6 min read
Multiline String in Python A sequence of characters is called a string. In Python, a string is a derived immutable data typeâonce defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.Python has multiple methods for defining strings. Single quotations (''), double
4 min read