Python String rjust() Method – Right Justify a String
Let’s look at string rjust() method – right justify a string
1
Notice
rjust() pads on the left so text is right-aligned in a minimum width.
2
Try
Follow the example, change its sample, and compare what happens.
3
Check
Explain the result and test your understanding.
Watch how it works
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
▷
Explore the idea
Follow the code and watch its state change. Guided simulation.
Code
Python
text = 'tea'
result = text.rjust(8, ".")
print(result)
Live state
Top-level scope
text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · str'.....tea'
This step
rjust() pads on the left so text is right-aligned in a minimum width.
Terminal
Output
(no output)
What do you expect this program to print?
◇
Picture string rjust() method – right justify a string
EXAMPLE INSTRUCTIONS
text = 'tea'
result = text.rjust(8, ".")
print(result)
|
v
DISPLAYED RESULT
.....tea
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'tea'
object 2: '.....tea'
rjust() pads on the left so text is right-aligned in a minimum width.
Image brief
Alt text
A diagram for Python String rjust() Method – Right Justify a String. Instructions: text = 'tea'
result = text.rjust(8, ".")
print(result). Displayed result: .....tea. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea'; object 2: '.....tea'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python String rjust() Method – Right Justify a String". Teaching focus: rjust() pads on the left so text is right-aligned in a minimum width. Depict exactly the relationships and labels in this reference diagram:
EXAMPLE INSTRUCTIONS
text = 'tea'
result = text.rjust(8, ".")
print(result)
|
v
DISPLAYED RESULT
.....tea
FINAL BINDINGS
text --> object 1 (str)
result --> object 2 (str)
object 1: 'tea'
object 2: '.....tea'
Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python String rjust() Method – Right Justify a String. Instructions: text = 'tea'
result = text.rjust(8, ".")
print(result). Displayed result: .....tea. The final example bindings are text --> object 1 (str); result --> object 2 (str). object 1: 'tea'; object 2: '.....tea'
Follow the execution
Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
▷
Execution workspace
Choose a sample and follow its execution. Guided simulation.
Code
Python
text = 'tea'
result = text.rjust(8, ".")
print(result)
Live state
Top-level scope
text→Object 1
result→Object 2
Object 1 · str'tea'Object 2 · str'.....tea'
This step
The example has finished. Compare the final output with the values and references above.
Terminal
Output
.....tea
What do you expect this program to print?
What does this example display?
text = 'tea'
result = text.rjust(8, ".")
print(result)
More examples and reference
Python String rjust() Method
The rjust() method in Python is used to right-align a string by padding it with a specified character (default is a space).
Syntax
string.rjust(width, fillchar=' ')
Parameters:
width – Total width of the resulting string (including original text + padding)
fillchar – (Optional) Character to use for padding (default is space)
Returns:
A new string that is right-aligned and padded with the specified character on the left.
Example 1: Right-justify with spaces (default)
text = "Python"
print(text.rjust(10))
Python
Example 2: Right-justify with a specific fill character
text = "42"
print(text.rjust(5, "0"))
00042
Use Case: Padding values for printing aligned output
items = ["Apple", "Banana", "Cherry"]
for item in items:
print(item.rjust(10))
Apple
Banana
Cherry
Common Mistakes
Using a fill character longer than one character (will raise a TypeError)
Expecting the original string to change (remember, strings are immutable)
Interview Tip
Use rjust() in formatting columns, table-like outputs, or aligning numbers for printing.
Summary
rjust() right-aligns the string by padding on the left.
You can customize the padding character using the fillchar parameter.
It returns a new string — original string is not modified.
Practice Problem
Write a program that accepts a word and prints it right-aligned in a field of width 20 using '*' as padding.
word = input("Enter a word: ")
print(word.rjust(20, '*'))
What to remember
Connect what you noticed in the example with the idea below.
Remember
rjust() pads on the left so text is right-aligned in a minimum width.
Watch out for this idea
rjust() pads on the right.
This is the misconception to avoid.
Reference checks are optional and do not count toward course progress.