Open In App

Python program to display half diamond pattern of numbers with star border

Last Updated : 21 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number n, the task is to write a Python program to print a half-diamond pattern of numbers with a star border.

Examples:

Input: n = 5
Output:

*
*1*
*121*
*12321*
*1234321*
*123454321*
*1234321*
*12321*
*121*
*1*
*


Input: n = 3
Output:

*
*1*
*121*
*12321*
*121*
*1*
*

Approach:

  • Two for loops will be run in this program in order to print the numbers as well as stars.
  • First print * and then run for loop from 1 to (n+1) to print up to the rows in ascending order.
  • In this particular for loop * will be printed up to i and then one more for loop will run from 1 to i+1 in order to print the numbers in ascending order.
  • Now one more loop will run from i-1 to 0 in order to print the number in the reverse order.
  • Now one star will be printed and this for loop will end.
  • Now second for loop will run from n-1 to 0 to print the pattern as in the middle in which the numbers are in a reverse manner.
  • In this for loop also the same work will be done as in first for loop.
  • The required pattern will be displayed.

Below is the implementation of the above pattern:

Python3




# function to display the pattern up to n
def display(n): 
   
    print("*")
     
    for i in range(1, n+1):
        print("*", end="")
         
        # for loop to display number up to i
        for j in range(1, i+1): 
            print(j, end="")
 
        # for loop to display number in reverse direction   
        for j in range(i-1, 0, -1): 
            print(j, end="")
 
        print("*", end="")
        print()
 
    # for loop to display i in reverse direction
    for i in range(n-1, 0, -1):
        print("*", end="")
        for j in range(1, i+1):
            print(j, end="")
 
        for j in range(i-1, 0, -1):
            print(j, end="")
 
        print("*", end="")
        print()
 
    print("*")
 
 
# driver code
n = 5
print('\nFor n =', n)
display(n)
 
n = 3
print('\nFor n =', n)
display(n)


Output

For n = 5
*
*1*
*121*
*12321*
*1234321*
*123454321*
*1234321*
*12321*
*121*
*1*
*

For n = 3
*
*1*
*121*
*12321*
*121*
*1*
*


Previous Article
Next Article

Similar Reads

Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Python Program to print hollow half diamond hash pattern
Give an integer N and the task is to print hollow half diamond pattern. Examples: Input : 6 Output : # # # # # # # # # # # # # # # # # # # # Input : 7 Output : # # # # # # # # # # # # # # # # # # # # # # # # Approach: The idea is to break the pattern into two parts: Upper part: For the upper half start the for loop with iterator (i) from 1 to n and
4 min read
Simple Diamond Pattern in Python
Given an integer n, the task is to write a python program to print diamond using loops and mathematical formulations. The minimum value of n should be greater than 4. Examples : For size = 5 * * * * * * * * * * * * For size = 8 * * * * * * * * * * * * * * * * * * * * * * * * * * For size = 11 * * * * * * * * * * * * * * * * * * * * * * * * * * * *
3 min read
Python Program to Print Christmas Tree Star Pattern
The "Python program to print Tree pattern" is a piece of code that creates a tree pattern out of asterisks (*) using Python. The indentation and amount of asterisks per row are controlled via loops and string manipulation to produce the tree pattern, which is presented on the console. Print Christmas Tree Star Pattern using LoopsThis program uses n
5 min read
Python Program to print a number diamond of any given size N in Rangoli Style
Given an integer N, the task is to print a number diamond of size N in rangoli style where N means till Nth number from number ‘1’. Examples: Input : 2 Output : --2-- 2-1-2 --2-- Input : 3 Output : ----3---- --3-2-3-- 3-2-1-2-3 --3-2-3-- ----3---- Input : 4 Output : ------4------ ----4-3-4---- --4-3-2-3-4-- 4-3-2-1-2-3-4 --4-3-2-3-4-- ----4-3-4----
3 min read
Python | Print an Inverted Star Pattern
An inverted star pattern involves printing a series of lines, each consisting of stars (*) that are arranged in a decreasing order. Here we are going to print inverted star patterns of desired sizes in Python Examples: 1) Below is the inverted star pattern of size n=5 (Because there are 5 horizontal lines or rows consist of stars). ***** **** *** *
2 min read
Draw Colourful Star Pattern in Turtle - Python
In this article we will use Python's turtle library to draw a spiral of stars, filled with randomly generated colours. We can generate different patterns by varying some parameters. modules required: turtle: turtle library enables users to draw picture or shapes using commands, providing them with a virtual canvas. turtle comes with Python's Standa
2 min read
Program to print the diamond shape
Given a number n, write a program to print a diamond shape with 2n rows. Examples : C/C++ Code // C++ program to print diamond shape // with 2n rows #include <bits/stdc++.h> using namespace std; // Prints diamond pattern with 2n rows void printDiamond(int n) { int space = n - 1; // run loop (parent loop) // till number of rows for (int i = 0;
11 min read
Display the Pandas DataFrame in table style and border around the table and not around the rows
Let us see how to style a Pandas DataFrame such that it has a border around the table. We will be using the set_table_styles() method of the Styler class in the Pandas module. set_table_styles() Syntax : set_table_styles(self, table_styles) Parameters : table_styles : List, each individual table_style should be a dictionary with selector and props
1 min read
bokeh.plotting.figure.diamond() function in Python
Bokeh is a data visualization library in Python that provides high-performance interactive charts and plots and the output can be obtained in various mediums like notebook, HTML and server. Figure Class create a new Figure for plotting. It is a subclass of Plot that simplifies plot creation with default axes, grids, tools, etc. bokeh.plotting.figur
2 min read
Practice Tags :
three90RightbarBannerImg