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:
- The outer loop for i in range(1,6) will iterate through the numbers 1 to 5.
- The inner loop for j in range(i) will iterate through the numbers 0 to i-1.
- 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.
- The print() statement outside the inner loop will add a newline after each row of numbers.
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()

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.
Related Topics
- Python Program to Check Leap Year
- Remove first n characters from a string | Python
- Check if the first and last number of a list is the same | Python
- Number of occurrences of a substring in a string in Python
- Remove last element from list in Python
- How to Use Modulo Operator in Python
- Enumerate() in Python
- Writing to a File using Python's print() Function
- How to read csv file in Python
- Dictionary Comprehension in Python
- How to Convert List to String in Python
- How to convert int to string in Python
- Random Float numbers in Python