Python Coding Exercise #60: Right-Aligned
Part of the series Python Practice Exercise — part 60
Before you start: Know input() and len() · Know string multiplication · Know that strings are immutable
The question
Please write a program which asks the user for a string and then prints it out so that exactly 20 characters are displayed. If the input is shorter than 20 characters, the beginning of the line is filled in with
*characters.You may assume the input string is at most 20 characters long.
Please type in a string: python
**************python
Please type in a string: alongerstring
*******alongerstring
Please type in a string: averyverylongstring
*averyverylongstring
Strings are immutable, but methods create new strings
As we learnt in Data Types in Python, a string is immutable: we can’t change it, or add characters to it, in place. But we can use a built-in string method to create a new string from it. The original string stays the same.
For this exercise, the method that does the job is rjust(). It right-aligns a string, which means that it pushes the text to the right and fills the empty space on the left. That is exactly what the exercise asks for.
There are many more string methods like this one, for changing case, trimming spaces, searching, splitting and more. I explain what each of them does, with examples, in this blog: String Methods in Python.
My Solution
words = input("Please type in a string: ")
print(words.rjust(20, "*"))
How My Solution Works
1. Read the string
words = input("Please type in a string: ") stores what the user typed.
2. rjust(20, "*") makes the new string
rjust() takes two arguments:
20is the width. The new string will be 20 characters long."*"is the fill character. It is added on the left to fill the space that the text doesn’t use. If it isn’t given, spaces are used.
So words.rjust(20, "*") returns a new string of 20 characters, with the text at the right end and asterisks in front.
3. Print the new string
print() shows the result. The variable words itself isn’t changed, because a method never changes the string it is called on.
Following the three samples
| The user types | Length | Asterisks added | Result |
|---|---|---|---|
python | 6 | 14 | **************python |
alongerstring | 13 | 7 | *******alongerstring |
averyverylongstring | 19 | 1 | *averyverylongstring |
The number of asterisks is always 20 minus the length of the text.
University Solution
word = input("Please type in a string: ")
aligned = (20 - len(word)) * "*" + word
print(aligned)
The University Solution gets the same result without a string method, by doing the work by hand:
len(word)is the length of the text. (See Exercise #30.)20 - len(word)is how many asterisks are needed to reach 20 characters.(20 - len(word)) * "*"makes that many asterisks, using string multiplication, as in Exercise #54.+ wordjoins the text to the end of the asterisks.
For python, that is 14 * "*" + "python", which gives **************python.
Both programs print identical output for the three samples and for every string length from 0 to 39 that was tested.
Method or by hand?
rjust() | By hand | |
|---|---|---|
| Lines of code | One | Two or more |
| What you need to know | That the method exists | len(), * and + |
| Easy to change the width | Change one number | Change the number in the calculation |
The by-hand version is a good way to understand what rjust() does behind the scenes. In practice, the method is shorter and easier to read.
Things to notice
The result is exactly 20 characters. For any text of 20 characters or less, the output is always 20 characters long. The text and the asterisks together fill the width.
An empty string gives 20 asterisks. If the user presses Enter without typing, the text is empty, so the whole line is asterisks.
A longer string is not cut. If a string is more than 20 characters long, rjust() returns it as it is, and the by-hand version does the same, because a negative number of asterisks is none at all. The exercise says the input is at most 20 characters, so this doesn’t come up.
The fill must be a single character. words.rjust(20, "**") stops with TypeError: The fill character must be exactly one character long.
There are matching methods for the other sides. ljust() pads on the right, and center() pads on both sides. They are explained in String Methods in Python.
Key takeaways
- Strings are immutable, but a string method returns a new string.
text.rjust(width, fill)right-aligns the text in the given width, and fills the left side.- The fill character is a space unless another one is given, and it must be exactly one character.
- The same result can be made by hand:
(width - len(text)) * fill + text. - A method doesn’t change the original string, so the result has to be printed or stored.
✦ This is part of my Python Practice Exercise series, where I work through exercises while learning Python and explain each answer in plain language. ✦
Practice Source: This exercise is taken from the Python Programming MOOC 2026 by the University of Helsinki. I’m working through the exercises as part of my Python learning journey and documenting my answers and explanations here.