How to Print the 1 12 123 Pattern in Python (5 Ways + 8 More Patterns)

To print the 1 12 123 pattern in Python, use two nested for loops: the outer loop picks the row number i, and the inner loop prints the numbers 1 to i on one line with print(j, end=""). Here is the complete program and its output, followed by four other ways to write it and eight related number patterns.

Every program on this page was run with Python 3.12.5, and the output shown is copied from the terminal. For the full reference, see for statements in the Python tutorial.

Python program to print the 1 12 123 pattern

rows = 5

for i in range(1, rows + 1):        # i = row number: 1, 2, 3, 4, 5
    for j in range(1, i + 1):       # j = 1 up to i
        print(j, end="")            # stay on the same line
    print()                         # move to the next line

Output:

1
12
123
1234
12345
Command Prompt screenshot of the Python 1 12 123 number pattern program with nested for loops and its output
The 1, 12, 123 pattern run in the Command Prompt.

Change rows = 5 to print more or fewer lines. Two details make it work: end="" stops print() from starting a new line after each number, and the empty print() after the inner loop ends the row.

How the 1 12 123 pattern in Python works: the outer loop sets the row i and the inner loop prints the numbers 1 to i
The outer loop chooses the row; the inner loop prints 1 up to that row number.

4 other ways to print 1 12 123 in Python

Method 1: join() in a single loop

Build each row as a string with str.join() and print it in one call. This is the most Pythonic version and needs only one explicit loop:

rows = 5

for i in range(1, rows + 1):
    print("".join(str(j) for j in range(1, i + 1)))

Output:

1
12
123
1234
12345

Method 2: Add one digit to a string

Each row is the previous row plus one more digit, so you can keep a running string and extend it:

rows = 5
line = ""

for i in range(1, rows + 1):
    line += str(i)                  # "1" -> "12" -> "123" ...
    print(line)

Output:

1
12
123
1234
12345

Method 3: Arithmetic, no strings

Multiplying by 10 and adding the next digit turns 1 into 12, 12 into 123 and so on. It is a neat trick, but it only works up to 9 rows, because 10 is two digits:

rows = 5
number = 0

for i in range(1, rows + 1):
    number = number * 10 + i        # 1 -> 12 -> 123 ...
    print(number)

Output:

1
12
123
1234
12345

Method 4: while loop

The same logic with while loops, where you increase the counters yourself. Useful if you are learning how loops work step by step:

rows = 5
i = 1

while i <= rows:
    j = 1
    while j <= i:
        print(j, end="")
        j += 1
    print()
    i += 1

Output:

1
12
123
1234
12345

Let the user choose the number of rows

Read the row count with input() and convert it with int():

rows = int(input("Enter the number of rows: "))

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end="")
    print()

Output (the user typed 4):

Enter the number of rows: 4
1
12
123
1234
Command Prompt screenshot of a Python number pattern where the user enters 4 rows
Entering 4 at the prompt prints four rows.

1 12 123 pattern with spaces: 1, 1 2, 1 2 3

Unpack the range into print() with *. print() puts a space between its arguments by default:

rows = 5

for i in range(1, rows + 1):
    print(*range(1, i + 1))         # print() puts a space between items

Output:

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

Use spaces when you need more than 9 rows. Without a separator, row 10 onwards becomes a run of digits that is hard to read:

rows = 12

for i in range(10, rows + 1):
    print("".join(str(j) for j in range(1, i + 1)))

Without spaces (rows 10 to 12):

12345678910
1234567891011
123456789101112
rows = 12

for i in range(1, rows + 1):
    print(*range(1, i + 1))

With spaces (12 rows):

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10 11 12

More number pattern programs in Python

These are the variations people ask about most. Each one changes only a line or two of the program above.

1 22 333 pattern

Repeat the row number i times. Multiplying a string repeats it:

rows = 5

for i in range(1, rows + 1):
    print(str(i) * i)               # "3" * 3 == "333"

Output:

1
22
333
4444
55555

12345 1234 123 12 1 pattern (reverse)

Count the outer loop down instead of up with range(rows, 0, -1):

rows = 5

for i in range(rows, 0, -1):        # 5, 4, 3, 2, 1
    for j in range(1, i + 1):
        print(j, end="")
    print()

Output:

12345
1234
123
12
1

Right-aligned 1 12 123 pattern

rjust() pads each row with spaces on the left so the numbers line up on the right:

rows = 5
line = ""

for i in range(1, rows + 1):
    line += str(i)
    print(line.rjust(rows))         # pad on the left to the full width

Output:

    1
   12
  123
 1234
12345

Number pyramid: 1 121 12321

Each row is the ascending part (123) followed by the same digits backwards without the last one (21), with spaces in front to centre it:

rows = 5

for i in range(1, rows + 1):
    left = "".join(str(j) for j in range(1, i + 1))    # 123
    right = left[-2::-1]                                # 21
    print(" " * (rows - i) + left + right)

Output:

    1
   121
  12321
 1234321
123454321

Floyd’s triangle: 1, 2 3, 4 5 6

Floyd’s triangle keeps counting across rows instead of restarting at 1:

rows = 4
n = 1

for i in range(1, rows + 1):
    print(*range(n, n + i))
    n += i

Output:

1
2 3
4 5 6
7 8 9 10

Star patterns

Replace the numbers with * for the classic right triangle:

rows = 5

for i in range(1, rows + 1):
    print("*" * i)

Output:

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

And a centred star pyramid, where row i has 2 * i - 1 stars:

rows = 5

for i in range(1, rows + 1):
    print(" " * (rows - i) + "*" * (2 * i - 1))

Output:

    *
   ***
  *****
 *******
*********
Command Prompt screenshot of a Python star pyramid pattern program and its output
The star pyramid run in the Command Prompt.

Common mistakes (and the output they produce)

Forgetting the print() after the inner loop puts everything on one line:

rows = 3

for i in range(1, rows + 1):
    for j in range(1, i + 1):
        print(j, end="")
    # forgot print() here

Wrong output:

112123

Using range(1, i) instead of range(1, i + 1) stops one number early, because range() excludes its end value, so the first row is empty:

rows = 3

for i in range(1, rows + 1):
    for j in range(1, i):           # stops one number too early
        print(j, end="")
    print()

Wrong output:


1
12

Why “123” and 123 behave differently

The string methods above work because "123" is text and 123 is a number. + joins strings but adds numbers, and * repeats a string but multiplies a number:

print("123" + "4")      # strings are joined
print(123 + 4)          # numbers are added
print("1" * 3)          # a string repeated
print(1 * 3)            # a number multiplied

Output:

1234
127
111
3

That is why the programs convert each number with str() before joining. See also how to print strings and integers together.

Which method should you use?

MethodBest forWatch out for
Nested for loopsLearning, exams, interviewsThe print() after the inner loop
join()Short, readable codeConvert numbers with str()
String buildingPatterns where each row extends the lastOnly works when rows grow by one item
ArithmeticA quick trickBreaks after 9 rows
while loopsUnderstanding loop countersForgetting to increase the counter (infinite loop)
print(*range(...))More than 9 rows, spaced outputAdds spaces between numbers

Frequently asked questions

How do I print 1 12 123 in Python?

Use two nested loops: for i in range(1, rows + 1): then for j in range(1, i + 1): print(j, end=""), and call print() after the inner loop. A shorter version is print("".join(str(j) for j in range(1, i + 1))) inside one loop.

How do I print the 1 22 333 pattern in Python?

Loop over the rows and repeat the row number as a string: for i in range(1, rows + 1): print(str(i) * i).

Why do we use end=”” in pattern programs?

print() adds a new line after every call by default. end="" replaces that new line with nothing, so the numbers of one row stay on the same line.

How do I print a number pattern with more than 9 rows?

Separate the numbers with spaces, for example print(*range(1, i + 1)). Without a separator, rows after 9 turn into long strings such as 12345678910 that are hard to read.

What is the difference between “123” and 123 in Python?

"123" is a string (text) and 123 is an integer. "123" + "4" gives "1234", while 123 + 4 gives 127.

Practice more Python programs: