print('.'*5).....
Salman Naqvi
October 12, 2026
![]()
Here, I demonstrate my process in working out a centered triangle. If you have a cooler way to print one, let me know!
I’ll begin by printing a regular 5x5 grid. A 5x5 grid has 5 rows. So I’ll work with rows. A row has 5 elements.
We repeat this 5 times to get a 5x5 grid.
We could make this into a one liner.
The next most simplest action I can take is to print a right triangle. This is matter of successively printing \(x+1\) items.
We need to adjust l by 1.
What if I want to print . as padding for those spaces where the triangle doesn’t exist? If a row contains 5 elements,
X and 4 .X and 3 .X and 2 .In 1 line.
Now if the output above is closely observed, the difference in having a neatly centered triangle is odd rows can only be output. The modulo operator % can help me determine what numbers are odd.
To center this triangle, I just have to split the padding on either side.
..X..
.XXX.
XXXXX
I don’t actually need to use % to determine what number is odd. The third parameter of range lets us define the step.
Python has built-in ways to justify text. > right aligns, < left aligns, and ^ centers.
If I don’t want to use whitespace to justify, Python has the rjust, ljust, and center methods.
----haha
haha----
--haha--
Back to the triangle.
In 1 line.
Justifying with another character.
If you have any comments, questions, suggestions, feedback, criticisms, or corrections, please do post them down in the comment section below!
Back to top