Saturday, October 4, 2025
HomeData Modelling & AIProgram to print a doormat pattern having a string written in the...

Program to print a doormat pattern having a string written in the center in Python

Given the number of rows R and the number of columns C, the task is to print a doormat pattern as shown in the examples below. The number of columns in the mat’s design must be 3 times the number of rows in the design. The doormat should have neveropen printed in the center. The width of the mat is equal to the number of columns in the pattern. Note that the string “neveropen” contains 13 characters which is an odd number, therefore the number of rows has to be odd to design the mat properly by maintaining the symmetricity. Examples:

Input: R = 11, C = 33 
Output:
    ---------------|*|---------------
    ------------|*||*||*|------------
    ---------|*||*||*||*||*|---------
    ------|*||*||*||*||*||*||*|------
    ---|*||*||*||*||*||*||*||*||*|---
    ----------neveropen----------
    ---|*||*||*||*||*||*||*||*||*|---
    ------|*||*||*||*||*||*||*|------
    ---------|*||*||*||*||*|---------
    ------------|*||*||*|------------
    ---------------|*|---------------

Input: R = 9, C = 27
Output:
    ------------|*|------------
    ---------|*||*||*|---------
    ------|*||*||*||*||*|------
    ---|*||*||*||*||*||*||*|---
    -------neveropen-------
    ---|*||*||*||*||*||*||*|---
    ------|*||*||*||*||*|------
    ---------|*||*||*|---------
    ------------|*|------------

Approach: The whole pattern can be divided into 3 different parts. The first part contains a pattern having a triangle. The second part contains a string “neveropen” and the third part contains a pattern having an inverted triangle. This pattern can easily be printed having a top and inverted triangle by using inbuilt string alignment functions.

  1. ljust : This method returns a left-aligned string having length l.
  2. center : This method returns a center-aligned string having length l.
  3. rjust : This method returns a right-aligned string having length l.

Below is the implementation of the above approach: 

Python3




# Python3 implementation of the approach
 
# Function to print the doormat pattern
def doormatPattern(rows, columns):
    width = columns
     
    # Print the pattern having a top triangle
    for i in range (0, int (rows / 2)):
        pattern = "|*|" * ((2 * i) + 1)
        print (pattern.center (width, '-'))
     
    # Print neveropen in the center
    print ("neveropen".center (width, '-'))
     
    # Print the pattern having
    # an inverted triangle
    i = int (rows / 2)
    while i > 0:
        pattern = "|*|" * ((2 * i) - 1)
        print (pattern.center (width, '-'))
        i = i-1
    return
 
# Driver code
rows = 7
columns = rows * 3
doormatPattern(rows, columns)


Output:

---------|*|---------
------|*||*||*|------
---|*||*||*||*||*|---
----neveropen----
---|*||*||*||*||*|---
------|*||*||*|------
---------|*|---------

Time complexity : O(n^2), where n is the number of rows.

Space complexity : O(n^2)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11870 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11934 POSTS0 COMMENTS
Shaida Kate Naidoo
6821 POSTS0 COMMENTS
Ted Musemwa
7086 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6778 POSTS0 COMMENTS