String alignment is commonly utilized in a wide range of everyday applications. Python’s language includes various functions that aid in string alignment. It also allows you to provide user-specified padding instead of blank space. The Python string API has two utility functions for generating a new string of a defined length from a source string with right and left justification. One of them being the Python rjust() function that is a common string handling method. We will learn more about Python rjust() and its programming applications in the article below.
Definition
- Python rjust() is a built-in function that returns a right-justified string according to the width specified and fills the remaining spaces with blank spaces if the character argument is not passed.
- The Python rjust() string method returns a right-padded string of a given minimum width while using a specific character to fill the additional position of the string.
Python rjust() Syntax
The syntax followed by Python rjust() function is as below:
string.rjust(width, fillchar)
rjust() Parameters
Python rjust() accepts 2 parameters:
- width – length of the string to be expanded. If the width is less than or equal to the total length of the string, then the original string is returned.
- fillchar (Optional) – character to be filled in the remaining spaces of the width. The default value is blank spaces.
Note – Only 1 specific character is to be mentioned in the fillchar argument. Else Python interpreter throws a TypeError exception.
Return value from rjust()
It returns a right-padded string with fillchar character added in place of blank spaces if the fillchar argument is specified.
Example 1: Right justify string of minimum width
Example
# Python program to illustrate rjust()
txt = 'Python'
print('Original string:', txt)
# right-justified string
r = txt.rjust(10)
print('Right-justified string:', r)
Output
Original string: Python
Right-justified string:Â Â Â Â Python
Explanation
In the above example, considering the first print statement, txt.rjust(10) generates a new string of length 10 and aligns ‘Python’ to the right. Because a fillchar is not specified, the default value space is utilized. So, the result is ‘Python’ with 4 spaces on the left side of the string.
Example 2: Right justify string and fill the remaining spaces
The input string is first aligned to the right, and then the remaining spaces on the left are filled with the provided character using the fillchar parameter.
Example
# Python program to illustrate rjust()
txt = 'Programming'
print('Original string:', txt)
# rigth-justified string with fillchar
r = txt.rjust(20, '*')
print('Right-justified string:', r)
txt = 'Hello'
print('Original string:', txt)
# right-justified string with fillchar
r = txt.rjust(10, '$')
print('Right-justified string:', r)
Output
Original string: Programming
Right-justified string: *********Programming
Original string: Hello
Right-justified string: $$$$$Hello
Example 3: TypeError Exception
The fillchar parameter must only contain one character. If more than one character is supplied as fillchar, it will throw a TypeError, as illustrated below.
Example
txt = 'Swimming'
Â
# rigth-justified string with multiple fillchar characters
r = txt.rjust(20, '->*')
print('Right-justified string:', r)
Output
Traceback (most recent call last):
File "", line 4, in
TypeError: The fill character must be exactly one character long
Frequently Asked Questions
Q1. How do you right justify in Python?
Python rjust() is a built-in function that returns a right-justified string according to the width specified and fills the remaining spaces with blank spaces if the character argument is not passed.
The rjust() function returns a new string of the specified length with the source string right-justified. The padding character can be specified; the default is whitespace. If the requested length is less than the length of the source string, the source string is returned.
Python rjust() function is as below:
string.rjust(width, fillchar)
Example
txt = 'Raining'
# right-justified string with fillchar
r = txt.rjust(15)
print('Right-justified string:', r)
# right-justified string with fillchar
r = txt.rjust(15, '-')
print('Right-justified string:', r)
Output
Right-justified string:Â Â Â Â Â Â Â Â Raining
Right-justified string: --------Raining
Q2. How do you left justify in Python?
The Python ljust() string method returns a left-padded string of a given minimum width while using a specific character to fill the additional position of the string. The syntax followed by the Python ljust() function is as below:
string.ljust(width, fillchar)
Example
txt = 'Raining'
# left-justified string with fillchar
r = txt.ljust(15)
print('Left-justified string:', r)
# left-justified string with fillchar
r = txt.ljust(15, '-')
print('Left-justified string:', r)
Output
Left-justified string: Raining
Left-justified string: Raining--------
Q3. How do you align text in Python?
To align text and strings in Python, we have 3 specific string handling methods. They are Python center(), Python rjust() and Python ljust(). They return a string that is at least width characters wide, which is constructed by padding the input string with the character fillchar (the default is a space) until the given width is reached on the right, left, or both sides. Let us look at each one of them and understand their working by looking at a program.
- Python ljust() – If the ‘fillchar’ parameter is not given, this function left aligns the string according to the width specified and fills the leftover space of the line with blank space.
- Python center() – If the ‘fillchar’ parameter is not given, this function center aligns the string according to the width specified and fills the leftover space of the line with blank space.
- Python rjust() – If the ‘fillchar’ parameter is not given, this function right aligns the string according to the width specified and fills the remaining space of the line with blank space.
Example
txt = 'I love ice cream'
# left-justified string with fillchar
r = txt.ljust(22, '$')
print('Left-justified string:', r)
# center-justified string with fillchar
r = txt.center(22, '^')
print('Center-justified string:', r)
# right-justified string with fillchar
r = txt.rjust(22, '!')
print('Right-justified string:', r)
Left-justified string: I love ice cream$$$$$$
Center-justified string: ^^^I love ice cream^^^
Right-justified string: !!!!!!I love ice cream
Q4. What does zfill mean in Python?
The zfill() function is a Python built-in function that pads 0s on the left side of a string to fill the specified width and returns a duplicate of the string. It generates a padded string by appending zeros. The Python string zfill() function effectively adds zeros (0) at the beginning of the string until it reaches the required length. The important thing to remember here is that if the length parameter value is less than the length of the string, no filling is performed.
Syntax
string.zfill(width)
Example
txt = 'Laptop'
r = txt.zfill(10)
print(r)
txt = '+Earth'
r = txt.zfill(10)
print(r)
Output
0000Laptop
+0000Earth
Leave a Reply