Alphabet Rangoli Pattern in Python
In this tutorial, we will learn how to print the alphabet rangoli pattern when the size of the pattern is given.
Alphabet Rangoli Pattern
When the size of the pattern is given we need to print the alphabet rangoli pattern. For instance, the size of the pattern is 3 then the alphabet rangoli pattern will be
----c---- --c-b-c-- c-b-a-b-c --c-b-c-- ----c----
The center of the pattern is the first alphabet letter a, and the boundary has the nth alphabet letter in alphabet order.
We are going to solve this by using the center method. The method center() method will center align the string, using a specified character as the fill character.
- Firstly create a list L and a constant alpha that contain lowercase alphabets [a-z]
- For each value in the range, create the pattern, and add it to the list.
- print each element in list L in a new line.
import string def rangoli_pattern(n): L = [] alpha = string.ascii_lowercase for i in range(n): s = "-".join(alpha[i:n]) L.append((s[::-1]+s[1:]).center(4*n-3, "-")) print('\n'.join(L[:0:-1]+L)) n = int(input("Enter the size of the pattern: ")) rangoli_pattern(n)
Output
Enter the size of the pattern: 5 --------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--------
Also, read
Leave a Reply