my_string.rjust(width, filler)
width : right justified string of width filler : (optional) string can be used to fill the width
my_str='Welcome to'
output=my_str.rjust(15,' ')
print(output, 'plus2net')
Output
Welcome to plus2net
my_str='Welcome to'
output=my_str.rjust(15,'#')
print(output, 'plus2net')
Output
#####Welcome to plus2net
# Aligning data in table format
header1 = "Name"
header2 = "Score"
print(header1.rjust(10) + header2.rjust(10))
data = [("Alice", 95), ("Bob", 87), ("Charlie", 92)]
for name, score in data:
print(name.rjust(10) + str(score).rjust(10))
output
Name Score
Alice 95
Bob 87
Charlie 92
Example : Right-Justify with Custom Padding
text = "Python"
print(text.rjust(10, '*')) # Output: ****Python
print(text.rjust(12, '-')) # Output: ------Python
header1 = "Item"
header2 = "Price"
print(header1.rjust(10) + header2.rjust(10))
items = [("Apple", 2.5), ("Banana", 1.1), ("Cherry", 3.2)]
for item, price in items:
print(item.rjust(10) + str(price).rjust(10))
output
Item Price
Apple 2.5
Banana 1.1
Cherry 3.2
All String methods ljust()
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.