Skip to content
Related Articles

Related Articles

Program to print a doormat pattern having a string written in the center in Python
  • Last Updated : 03 Jan, 2020

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 GeeksforGeeks printed in the center. The width of the mat is equal to the number of columns in the pattern.
Note that the string “GeeksforGeeks” 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:
    ---------------|*|---------------
    ------------|*||*||*|------------
    ---------|*||*||*||*||*|---------
    ------|*||*||*||*||*||*||*|------
    ---|*||*||*||*||*||*||*||*||*|---
    ----------GeeksforGeeks----------
    ---|*||*||*||*||*||*||*||*||*|---
    ------|*||*||*||*||*||*||*|------
    ---------|*||*||*||*||*|---------
    ------------|*||*||*|------------
    ---------------|*|---------------

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

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 “GeeksforGeeks” 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 GeeksforGeeks in the center
    print ("GeeksforGeeks".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)

chevron_right


Output:

---------|*|---------
------|*||*||*|------
---|*||*||*||*||*|---
----GeeksforGeeks----
---|*||*||*||*||*|---
------|*||*||*|------
---------|*|---------

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :