Difficult Python Question 3 — Hollow Triangle Of Letters

Write a function that takes in a string, and prints the following hollow triangular pattern. If there are insufficient characters to make a perfect triangle, simply use * characters in their place.
string = “abcdefghijkl” (length=12)
Here, this string can form a perfect triangle by itself
a
b l
c k
defghijstring = “abcdefghijk” (length=11)
Here, there are insufficient letters to form a perfect triangle, so * characters are used to fill it up.
a
b *
c k
defghijstring = “abcdefghij” (length=10)
a
b *
c *
defghijstring = “abcdefghi” (length=9)
a
b *
c *
defghi*string = “abcdefgh” (length=8)
Here, this string can form a perfect triangle by itself
a
b h
cdefgstring = “abcdefghijklmnop” (length=16)
This string too can form a perfect triangle by itself.
a
b p
c o
d n
efghijklmstring = “abcdefghijklmno” (length=15)
a
b *
c o
d n
efghijklmSome Hints:
- Notice that strings with lengths that are multiples of 4 make perfect triangles (no
*needed) - Add the needed
*characters to your string first - Try to find a relationship between the length of the string and the height of the triangle
- Split your function into 3 parts — 1 to print the first line, 1 to print the last line, and 1 to print all the middle lines
The Logic Behind The Solution
Let’s split the solution into a couple of steps
- Adding the additional
*to our string - Finding the height of our hollow triangle
- Establishing patterns to print our triangle
- Printing the first line
- Printing the 2nd line to the 2nd last line
- Printing the last line
1) Adding the additional * to our string
Here, we know that strings with a length that is a multiple of 4 will make a perfect triangle. As such, we want to add enough * characters behind our string such that its length becomes a multiple of 4.
Let’s say we have a string "abcdefghi" which has a length of 9. To make its length a multiple of 4, we would need to add 3 * characters. Now let’s say we have a string "abcdefghijk" which has a length of 11 characters. To make its length a multiple of 4, we would need to add only 1 * character.
Hence, to determine how many * to add, we can use the modulo % operator, which gives us the remainder when one number is divided by another, and we arrive at this formula 4 — len(string) % 4). At the end of this step, our string should have 0 to 4 * characters added behind to make its length a multiple of 4.
2) Finding the height of our hollow triangle
Let’s list down some string lengths and their corresponding triangle height
string length of 8 -> triangle height of 3
string length of 12 -> triangle height of 4
string length of 16 -> triangle height of 5Here, we can see this relationship:
triangle height = string length // 4 + 13) Establishing patterns to print our triangle
Let’s say we have a string "abcdefghijklmno*" after we’ve added the necessary * characters. Our triangle with height of 5:
a
b *
c o
d n
efghijklmObservable patterns:
a 4 spaces + string[0]
b * 3 spaces + string[1] + 1 spaces + string[-1]
c o 2 spaces + string[2] + 3 spaces + string[-2]
d n 1 spaces + string[3] + 5 spaces + string[-3]
efghijklm string[4: -4+1]Based on the patterns here, we can split the function into 3 parts:
4) Printing the first line
a 4 spaces + string[0]The first line consists of (height-1) spaces and the first character (character at index 0), and we simply need to print this once.
5) Printing the 2nd line to the 2nd last line
b * 3 spaces + string[1] + 1 spaces + string[-1]
c o 2 spaces + string[2] + 3 spaces + string[-2]
d n 1 spaces + string[3] + 5 spaces + string[-3]Here, we need a for loop that loops height-2 times (minus the 1st and last line). For each line, we need to print:
height-i-1spacesstring[i]i*2-1spacesstring[-i]
6) Printing the last line
efghijklm string[4: -4+1]The last line consists of all characters that we haven’t printed — in this case it would be the slice from 4 to -3.
The Code Solution
def hollow_triangle(string):
if len(string) % 4 > 0:
string += "*" * (4-len(string)%4) height = len(string) // 4 + 1 print(" "*(height-1) + string[0])
for i in range(1, height-1):
print(" "*(height-i-1), end="")
print(string[i], end="")
print(" "*(2*i-1), end="")
print(string[-1]) print(string[i+1:-i])The Code Solution (Comments Added)
def hollow_triangle(string): # step 1 - adding * to string
if len(string) % 4 > 0:
string += "*" * (4-len(string)%4) # step 2 - finding height of triangle
height = len(string) // 4 + 1 # step 4 - printing first line
print(" "*(height-1) + string[0])
# step 5 - printing 2nd to 2nd last line
for i in range(1, height-1):
print(" "*(height-i-1), end="")
print(string[i], end="")
print(" "*(2*i-1), end="")
print(string[-1]) # step 6 - printing last line
print(string[i+1:-i])Conclusion
I write coding articles (once per 1–2 days) that would have probably helped the younger me speed up my learning curve. Do join my email list to get notified whenever I publish.
If this article provided value and you wish to support me, do consider signing up for a Medium membership — It’s $5 a month, and you get unlimited access to articles on Medium. If you sign up using my link below, I’ll earn a tiny commission at zero additional cost to you.
Sign up using my link here to read unlimited Medium articles
If this article provided immense value for you, do consider buying me a coffee — every small contribution is appreciated greatly!





