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 is used to output text or other data to the console. The text or data that you want to print is passed as an argument to the print() function. For example: print("Hello, world!") would output the text "Hello, world!" to the console. You can also use print() to print the value of a variable, for example: x = 5; print(x) would output the value of the variable x, which is 5.