Print the following pattern using python

1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Here's one way to print the pattern using a nested loop:

for i in range(1,6): for j in range(i): print(i, end=" ") print()
Output:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Explanation:

  1. The outer loop for i in range(1,6) will iterate through the numbers 1 to 5.
  2. The inner loop for j in range(i) will iterate through the numbers 0 to i-1.
  3. The print(i, end=" ") statement inside the inner loop will print the current value of i and the end=" " will add a space after the number instead of a newline.
  4. The print() statement outside the inner loop will add a newline after each row of numbers.

You can also use the * operator to repeat the number i times in each row.

for i in range(1,6): print(str(i)*i)
Output:
1 22 333 4444 55555

Using Python String join() Method

You can also use join() method of a string to join the repeated characters.

for i in range(1,6): print(" ".join([str(i)]*i))
Output:
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5

Python print()


Python print function

In Python, the print() function serves as a fundamental tool for displaying text or data to the console. This function receives the desired text or data as an argument, enabling the programmer to effortlessly produce output. For instance, by calling print("Hello, world!"), the console would exhibit the message "Hello, world!".

Furthermore, print() can be utilized to exhibit the value of a variable. For instance, if we have the variable x assigned as x = 5, invoking print(x) would produce the value of x, which is 5, in the console.