How do you print a hollow star pattern in python?

This python program generates hollow pyramid pattern made up of stars up to n lines.

In this python example, we first read number of row in the hollow pyramid pattern from user using built-in function input(). And then we use using python's for loop to print hollow pyramid pattern.

Python Source Code: Pyramid Pattern


# Generating Hollow Pyramid Pattern Using Stars

row = int(input('Enter number of rows required: '))

for i in range(row):
    for j in range(row-i):
        print(' ', end='') # printing space required and staying in same line
    
    for j in range(2*i+1):
        if j==0 or j==2*i or i==row-1:
            print('*',end='')
        else:
            print(' ', end='')
    print() # printing new line

In this program print() only is used to bring control to new lines.

Output: Pyramid Pattern

Enter number of rows required: 12
            *
           * *
          *   *
         *     *
        *       *
       *         *
      *           *
     *             *
    *               *
   *                 *
  *                   *
 ***********************

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Hollow rectangle star pattern :

    The task is print below hollow pattern of given dimension. 
     

    ********************
    *                  *
    *                  *
    *                  *
    *                  *
    ********************

    Explanation: 
     

    • Input number of rows and columns.
    • For rows of rectangle run the outer loop from 1 to rows. 
       
    for (i = 1; i < = rows; i++)
    • For column of rectangle run the inner loop from 1 to columns. 
       
    for (j = 1; j < = columns; j++)
    • Print star for first or last row or for first or last column, otherwise print blank space.
    • After printing all columns of a row, print new line after inner loop.

    C++

    #include <bits/stdc++.h>

    using namespace std;

    void print_rectangle(int n, int m)

    {

        int i, j;

        for (i = 1; i <= n; i++)

        {

            for (j = 1; j <= m; j++)

            {

                if (i == 1 || i == n ||

                    j == 1 || j == m)        

                    cout << "*";            

                else

                    cout << " ";

            }

            cout << endl;

        }

    }

    int main()

    {

        int rows = 6, columns = 20;

        print_rectangle(rows, columns);

        return 0;

    }

    C

    #include <stdio.h>

    void print_rectangle(int n, int m)

    {

        int i, j;

        for (i = 1; i <= n; i++)

        {

            for (j = 1; j <= m; j++)

            {

                if (i==1 || i==n || j==1 || j==m)           

                    printf("*");           

                else

                    printf(" ");           

            }

            printf("\n");

        }

    }

    int main()

    {

        int rows = 6, columns = 20;

        print_rectangle(rows, columns);

        return 0;

    }

    Java

    import java.io.*;

    class GFG {

        static void print_rectangle(int n, int m)

        {

            int i, j;

            for (i = 1; i <= n; i++)

            {

                for (j = 1; j <= m; j++)

                {

                    if (i == 1 || i == n ||

                        j == 1 || j == m)           

                        System.out.print("*");           

                    else

                        System.out.print(" ");           

                }

                System.out.println();

            }

        }

        public static void main(String args[])

        {

            int rows = 6, columns = 20;

            print_rectangle(rows, columns);

        }

    }

    Python3

    def print_rectangle(n, m) :

        for i in range(1, n+1) :

            for j in range(1, m+1) :

                if (i == 1 or i == n or

                    j == 1 or j == m) :

                    print("*", end="")           

                else :

                    print(" ", end="")           

            print()

    rows = 6

    columns = 20

    print_rectangle(rows, columns)

    C#

    using System;

    public class GFG

    {

      static void print_rectangle(int n, int m)

      {

        int i, j;

        for (i = 1; i <= n; i++)

        {

          for (j = 1; j <= m; j++)

          {

            if (i == 1 || i == n ||

                j == 1 || j == m)           

              Console.Write("*");           

            else

              Console.Write(" ");           

          }

          Console.WriteLine();

        }

      }

      public static void Main()

      {

        int rows = 6, columns = 20;

        print_rectangle(rows, columns);

      }

    }

    PHP

    <?php

    function print_rectangle($n, $m)

    {

        $i;

        $j;

        for ($i = 1; $i <= $n; $i++)

        {

            for ($j = 1; $j <= $m; $j++)

            {

                if ($i == 1 || $i == $n ||

                    $j == 1 || $j == $m)        

                    echo("*");        

                else

                    echo(" ");        

            }

            echo("\n");

        }

    }

        $rows = 6;

        $columns = 20;

        print_rectangle($rows, $columns);

    ?>

    Javascript

    <script>

          function print_rectangle(n, m)

          {

            var i, j;

            for (i = 1; i <= n; i++)

            {

              for (j = 1; j <= m; j++)

              {

                if (i == 1 || i == n || j == 1 || j == m)

                     document.write("*");

                else

                    document.write("  ");

              }

              document.write("<br>");

            }

          }

          var rows = 6,

            columns = 20;

          print_rectangle(rows, columns);

        </script>

    Output: 
     

    ********************
    *                  *
    *                  *
    *                  *
    *                  *
    ********************

    Time Complexity: O(m * n), where m and n represents the given inputs.
    Auxiliary Space: O(1), no extra space is required, so it is a constant.

    Hollow square star pattern :

    ********
    *      *
    *      *
    *      *
    *      *
    *      *
    *      *
    ********

    Explanation: 
     

    • Input number of rows.
    • For rows, an outer loop from 1 to N.
    • For columns, an inner loop from 1 to N
    • Inside inner loop print star for first and last row or for first and last column. Which is print star 
       
     if i == 1 or i == N or j == 1 or j == N
    • otherwise print space.
    • After printing all columns of a row, print a blank line after inner loop. 
       

    C++

    #include <bits/stdc++.h>

    using namespace std;

    void print_square(int n)

    {

        int i, j;

        for (i = 1; i <= n; i++)

        {

            for (j = 1; j <= n; j++)

            {

                if (i==1 || i==n || j==1 || j==n)    

                    cout << "*";        

                else   

                    cout << " ";        

            }

            cout << "\n";

        }

    }

    int main()

    {

        int rows = 8;

        print_square(rows);

        return 0;

    }

    C

    #include <stdio.h>

    void print_square(int n)

    {

        int i, j;

        for (i = 1; i <= n; i++)

        {

            for (j = 1; j <= n; j++)

            {

                if (i==1 || i==n || j==1 || j==n)           

                    printf("*");           

                else           

                    printf(" ");           

            }

            printf("\n");

        }

    }

    int main()

    {

        int rows = 8;

        print_square(rows);

        return 0;

    }

    Java

    import java.io.*;

    class GFG {

        static void print_square(int n)

        {

            int i, j;

            for (i = 1; i <= n; i++)

            {

                for (j = 1; j <= n; j++)

                {

                    if (i == 1 || i == n ||

                        j == 1 || j == n)           

                        System.out.print("*");           

                    else          

                        System.out.print(" ");           

                }

                System.out.println();

            }

        }

        public static void main(String args[])

        {

            int rows = 8;

            print_square(rows);

        }

    }

    Python3

    def print_square(n) :

        for i in range(1, n+1) :

            for j in range(1, n+1) :

                if (i == 1 or i == n or

                    j == 1 or j == n) :           

                    print("*", end = "")           

                else :

                    print(" ", end = "")           

            print()

    rows = 8

    print_square(rows)

    C#

    using System;

    class GFG {

        static void print_squaredi(int n)

        {

            int i, j;

            for (i = 1; i <= n; i++)

            {

                for (j = 1; j <= n; j++)

                {

                    if (i == 1 || i == n ||

                        j == 1 || j == n ||

                        i == j || j == (n - i + 1))        

                        Console.Write("*");        

                    else       

                    Console.Write(" ");        

                }

                Console.WriteLine();

            }

        }

        public static void Main()

        {

            int rows = 8;

            print_squaredi(rows);

        }

    }

    PHP

    <?php

    function print_squaredi($n)

    {

        $i;

        $j;

        for($i = 1; $i <= $n; $i++)

        {

            for ($j = 1; $j <= $n; $j++)

            {

                if($i == 1 or $i == $n or $j == 1 ||

                   $j == $n or $i == $j or $j == ($n -

                                              $i + 1))    

                    printf("*");        

                else   

                    echo " ";    

            }

            echo "\n";

        }

    }

        $rows = 8;

        print_squaredi($rows);

    ?>

    Javascript

    Javascript   

        function print_square(int n)

        {

            var i, j;

            for (i = 1; i <= n; i++)

            {

                for (j = 1; j <= n; j++)

                {

                    if (i == 1 || i == n ||

                        j == 1 || j == n)           

                        document.write("*");           

                    else          

                        document.write(" ");           

                }

                document.write("\n");

            }

        }

            var rows = 8;

            print_square(rows);

    Output: 
     

    ********
    **    **
    * *  * *
    *  **  *
    *  **  *
    * *  * *
    **    **
    ********

    Time Complexity: O(n2), where n represents the given input.
    Auxiliary Space: O(1), no extra space is required, so it is a constant.


    How do you make a star pattern in Python?

    Code -.
    n = int(input("Enter the number of rows: ")).
    m = (2 * n) - 2..
    for i in range(0, n):.
    for j in range(0, m):.
    print(end=" ").
    m = m - 1 # decrementing m after each loop..
    for j in range(0, i + 1):.
    # printing full Triangle pyramid using stars..

    How do you print a hollow pattern?

    An algorithm is given below to explain how to print hollow rectangle star(*) pattern by using for loop. Step 1 − Input number of rows to print at runtime. Step 2 − Use outer for loop for rows from 1 to N. Step 3 − Run an inner loop from 1 to N for columns.

    How do you print a hollow pyramid pattern in Python?

    3 comments on “Python Program for Printing Hollow Pyramid Star Pattern”.
    Khushboo. n = int(input(“Enter the number:”)) for i in range(n): if ((i==0) or (i==n-1)): ... .
    mahesh. can solve even simply. num = int(input(“Enter the Number: “)) for i in range(0,num): ... .
    Rahul. num = int(input(“enter number:”)) for i in range(1, num):.

    How do you print the star symbol in Python?

    Print star or number Use the print() function in each iteration of nested for loop to display the symbol or number of a pattern (like a star (asterisk * ) or number).