Last Updated : 30 Dec, 2024
The rjust() method in Python is a string method used to right-align a string within a specified width by padding it with a specified character (default is a space). This method is helpful when you want to align text or numbers for formatting purposes.
Python
s = 'geeks'
res = s.rjust(10)
print(res)
Explanation:
Syntax: string.rjust(length, fillchar)
Parameters:
Return: Returns a new string of given length after substituting a given character in left side of original string.
Example 1: Using a Custom Padding CharacterIn this example, original string "Python" has a length of 6 and width is specified as 12, so 6 '-' characters are added to the left to make the total length 12.
Python
# example string
string = 'geeks'
length = 8
fillchar = '*'
print(string.rjust(length, fillchar))
Output:
***geeksExample 2: Width Less Than String Length
In this example, specified width (3) is less than the length of the string (5), so the original string is returned without any padding.
Python
s = "World"
res = s.rjust(3)
print(res)
Example 3: Aligning Numbers
In the below example, each number is right-aligned within a width of 5. The fillchar is specified as '0', so zeros are added to the left to fill the space.
Python
n = ["10", "200", "3"]
for i in n:
print(i.rjust(5, '0'))
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4