Print rectangle number pattern in python

This python program generates or prints rectangular pattern made up of stars up to n lines.

Python Source Code: Rectangular Pattern


# Rectangular pattern in Python

# Reading number of rows and columns
row = int[input["Enter number of rows: "]]
col = int[input["Enter number of columns: "]]

print["Rectangular pattern is: "]
for i in range[1,row+1]:
    for j in range[1,col+1]:
        print["*", end=""]
    print[]

Output: Rectangular Pattern

Enter number of rows: 4
Enter number of columns: 12
Rectangular pattern is: 

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

In this shot, we will discuss how to generate a rectangle pattern using numbers in Python.

Numerous patterns can be printed using Python once we have a strong grasp on concepts involving loops. Here, we will be using simple for loops to generate a rectangle pattern using numbers.

Description

To execute a rectangular pattern using Python programming, we will be using two for loops, one outer and one nested loop:

  • Outer loop: This is used to iterate over the number of rows.
  • Inner nested loop: This is used to iterate over the number of columns in each row.

Code

Let’s look at the below code snippet.

# Initialising Length and Breadth

rows = 3

columns = 6

# Loop through number of rows

for i in range[rows]:

# Loop through number of columns

for j in range[columns]:

# Printing Pattern

print[i+1, end = ' ']

print[]

Explanation

  • In line 2, we have taken the input for the number of rows.
  • In line 3, we have taken the input for the number of columns.
  • In line 6, we have created a for loop to iterate through the number of rows.
  • In line 9, we have created a for loop to iterate through the number of columns.
  • In line 12, i+1 has been used so as to start the pattern from 1. i increases with the increase in row number.

CONTRIBUTOR

Vinisha Maheshwari

In this shot, we will discuss how to generate a rectangle pattern using stars in Python. Numerous patterns can be printed using python, once we have a strong grip over the concepts involving loops. Here we will be using simple for loops to generate a rectangle pattern using stars.

Description

To execute a rectangular pattern using Python programming, we will use 2 for loops:

  • Outer loop: It is used to iterate over the number of rows.
  • Inner nested loop: It is used to iterate over the number of columns in each row.

Code

Let’s look at the code snippet below.

# Initialising Length and Breadth

rows = 3

columns = 6

# Loop through number of rows

for i in range[rows]:

# Loop through number of columns

for j in range[columns]:

# Printing Pattern

print['*', end = ' ']

print[]

Explanation

  • In line 2, we take the input for the number of rows.
  • In line 3, we take the input for the number of columns.
  • In line 6, we create a for loop to iterate through the number of rows.
  • In line 9, we create a for loop to iterate through the number of columns.
  • In lines 12 and 13, we print the pattern.
    • The end statement is used to stay on the same line.
    • The print[] statement is used to move to the next line.

CONTRIBUTOR

Vinisha Maheshwari

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given height h and width w, print a rectangular pattern as shown in the example below.

    Examples:

    Input  : h = 4, w = 5
    Output : @@@@@
             @   @
             @   @
             @@@@@
    
    Input  : h = 7, w = 9
    Output : @@@@@@@@
             @      @
             @      @
             @      @
             @      @
             @      @
             @@@@@@@@

    The idea is to run two loops. One for the number of rows to be printed and the other for the number of columns. Print a ‘@’ only when the current row is first or last. OR the current column is first or last. 

    C++

    #include

    using namespace std;

    void printRectangle[int h, int w]

    {

        for [int i=0; i

    Output

    @@@@@
    @   @
    @   @
    @@@@@

    Time complexity: O[n2], Space Complexity: O[1] //  Constant space.

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

    Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.


    How do you print a rectangle number pattern in Python?

    To execute a rectangular pattern using Python programming, we will be using two for loops, one outer and one nested loop: Outer loop: This is used to iterate over the number of rows. Inner nested loop: This is used to iterate over the number of columns in each row.

    How do you print a rectangle pattern?

    Step by step descriptive logic to print rectangle star pattern..
    Input number of rows and columns from user. ... .
    To iterate through rows, run an outer loop from 1 to rows . ... .
    To iterate through columns, run an inner loop from 1 to columns . ... .
    Inside inner loop print star * ..
    After printing all columns of a row..

    How do I print a specific pattern in Python?

    Steps to Print Pattern in Python.
    Decide the number of rows and columns. There is a typical structure to print any pattern, i.e., the number of rows and columns. ... .
    Iterate rows. ... .
    Iterate columns. ... .
    Print star or number. ... .
    Add new line after each iteration of outer loop..

    Chủ Đề