Creating Alphabet Rangoli with Python – A Step-by-Step Breakdown

Michelle WayuaMichelle Wayua
5 min read

Problem Statement

You're given a integer N. Your task is to print an alphabet rangoli of size N, using lowercase letters from the English alphabet. The pattern must be symmetrical and centred properly.

Sample Outputs

Alphabet Rangoli of size 3

Alphabet Rangoli of size 5

What is an Alphabet Rangoli ?

An "alphabet rangoli" is a specific type of rangoli, a traditional Indian art form, that utilizes the letters of the alphabet to create a decorative pattern.

In this coding challenge, we’ll generate a rangoli pattern with lowercase English letters, centred around the letter 'a' in the middle, and expanding outwards to the given size. We’ll explore how to use string slicing, list manipulation, and formatting techniques in Python to achieve this pattern. From understanding the structure and symmetry of the rangoli to breaking down the logic behind the code, you’ll learn the fundamentals of string handling while solving this fun challenge. Along the way, we’ll discuss the different methods used to format, centre, and join strings to create the desired output, making it an excellent exercise for enhancing your Python skills.

The Solution Code

def print_rangoli(size):
    import string
    letters = string.ascii_lowercase
    pattern = []

    for i in range(size):
        left = letters[size-1:i:-1] #left side - from current letter down to letter 'a'
        right = letters[i:size] #right side - from current letter to end letter
        row = '-'.join(left + right) #combine both sides

        row = row.center(4*size - 3, '-') #center the row to make symmetrical pattern
        pattern.append(row)

    # Mirror the top half to create the full rangoli
    full_rangoli = pattern[::-1] + pattern[1:]

    # Print each line
    for row in full_rangoli:
        print(row)

print_rangoli(3)

Deep Dive: Breaking Down Each Line of the Code.

def print_rangoli(size):

This line defines function named ‘print_rangoli‘ that takes an integer input ‘size’. The size determines how big the rangoli is, it sets the outermost letter e.g. size 5 is letter ‘e‘, size 3 is letter ‘c‘ etc.

import string

This line imports Python’s string module, which contains useful string constants. We use it to get lowercase letters: string.ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'.

letters = string.ascii_lowercase

Here, we’re storing all lowercase letters (a to z) in the variable named ‘letters‘.

pattern = []

This line initializes an empty list to hold each line (string) of the rangoli. We'll build the rangoli line-by-line and store them here for combining later.

    for i in range(size):

This line loops from 0 to size - 1. Each i represents the current line we're building from outer to centre.

left = letters[size-1:i:-1]

This is Python string slicing with a step of -1, which means it's reading the string backwards from index size -1 down to index i (but not including index i itself).

For Example: Assume;

letters = ‘abcdefghijklmnopqrstuvwxyz‘ and lets say size is 5, meaning we are using letters ‘a‘ to ‘e‘ (in indexing, remember index begins at 0, its index 0 - 4).

So, letters[4] = 'e', letters[3] = ‘d‘ and so on.

The string slicing [size-1:i:-1] uses the start:stop:step format and excludes the stop index.

So if i = 0, then

letters[4:0:-1] = 'e', 'd', 'c', 'b' #stops before index 0.

if i = 1, then

letters[4:1:-1] = 'e', 'd', 'c' #stops before index 1.
right = letters[i:size]

This line extracts the right half from the current letter to the boundary by slicing the alphabet from index i to size - 1, including the start but excluding the end. In other words, letters[i:size] means 'start at index i and go up to, but not include, index size.

For example; size = 5, i = 2, then

letters = 'c', 'd', 'e'
row = '-'.join(left + right)

This line joins both sides of the row with dashes.

So, in our example where the size is 5 and i is 2, the left and right sides will appear as follows:

size = 5
i = 2
#in left = letters[size-1:i:-1]
left = 'e', 'd'

#in right = letters[i:size]
right = 'c', 'd', 'e'

#so when combining both left and right usind a '-', our third row(index 2) will be
row = e-d-c-d-e
    row = row.center(4*size - 3, '-') #center the row to make symmetrical pattern

This line is responsible for centering each row of the rangoli with hyphens (-) so the overall pattern forms a perfect symmetrical diamond.

The str.center(width, fillchar) method is a built-in Python method used to center a string within a specified width by padding it with the character of your choice (default is space, but here it’s a ‘-‘). For example; “abc“.center(7, ‘-‘) will be '--abc--' .

In our case, its (4 * size - 3, ‘-‘). Given size 5, each row should be 17 characters wide. For row 3 which our output was e-d-c-d-e it will be; ----e-d-c-d-e----

pattern.append(row)

This line add the row to the pattern list.

  # Mirror the top half to create the full rangoli
    full_rangoli = pattern[::-1] + pattern[1:]

This line is key to creating the symmetric, diamond-like shape of the alphabet rangoli by mirroring the top half to form the bottom half.

Earlier in the code, during the for loop, we built a list called pattern. So if size is 5, the pattern list contains:

--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e

This is only the top half part (including the middle line) of the rangoli.

To complete the full rangoli, we want to:

  • Keep the top half as it is

  • Add a mirrored bottom half (which should not repeat the middle line)

So, basically this line of code does this:

  1. pattern[::-1] - reverses the entire list (like flip it upside down)

     e-d-c-b-a-b-c-d-e
     --e-d-c-b-c-d-e--
     ----e-d-c-d-e----
     ------e-d-e------
     --------e--------
    
  2. pattern[1:] - slices off the first line of the top half, keeping everything from the second line onward

       ------e-d-e------
       ----e-d-c-d-e----
       --e-d-c-b-c-d-e--
       e-d-c-b-a-b-c-d-e
    
  3. pattern[::-1] + pattern[1:]

     --------e--------
     ------e-d-e------
     ----e-d-c-d-e----
     --e-d-c-b-c-d-e--
     e-d-c-b-a-b-c-d-e
     --e-d-c-b-c-d-e--
     ----e-d-c-d-e----
     ------e-d-e------
     --------e--------
    
    for row in full_rangoli:
        print(row)

This prints the complete rangoli line by line.

All done ! ✨

🎯 Final Takeaway

The Alphabet Rangoli challenge is a great way to practice string manipulation and pattern building in Python. I hope this breakdown helped clarify the logic and inspired you to explore more creative coding tasks.

Follow me on GitHub fun Python projects!

0
Subscribe to my newsletter

Read articles from Michelle Wayua directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Michelle Wayua
Michelle Wayua