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 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----
---|*||*||*||*||*|---
------|*||*||*|------
---------|*|---------

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.