Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Print the 1 12 123 Pattern in Python: 5 Ways and 8 Variations

Print 1, 12, 123 in Python with a beginner-friendly nested-loop solution, four alternatives, and eight related pattern variations.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a nested loop to print 1, 12, 123, and later rows: the outer loop selects the row, and the inner loop prints each number from 1 through that row’s number. The examples below show five approaches, then eight related patterns.

The basic nested-loop solution

rows = 5

for row in range(1, rows + 1):
    for value in range(1, row + 1):
        print(value, end="")
    print()

Output:

1
12
123
1234
12345

rows sets the number of lines. The outer loop assigns each line a number from 1 through rows; the inner loop prints values from 1 through the current row number. Python’s range(start, stop) includes the start but excludes the stop, so row + 1 makes the current row number part of the sequence. The Python 3.14.8 tutorial explains that the endpoint is not included in a generated range.

As an Amazon Associate I earn from qualifying purchases.

print(value, end="") keeps the values together on one line instead of adding a newline after each value. Once the inner loop finishes, the standalone print() moves output to the next line. The same line-ending behavior is used in the direct pattern example from Python Guides.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Five ways to create the pattern

These approaches produce the same ascending triangle except for the fifth, which deliberately makes a repeated-digit pattern. The nested for loop is the clearest starting point for learning how the row and value loops work.

Method Code Best suited to
1. Nested for loops
rows = 5
for row in range(1, rows + 1):
    for value in range(1, row + 1):
        print(value, end="")
    print()
Seeing both loop boundaries and how each row is built.
2. Nested while loops
rows = 5
row = 1
while row <= rows:
    value = 1
    while value <= row:
        print(value, end="")
        value += 1
    print()
    row += 1
Practicing explicit counter setup and increments. Reset value to 1 for each row.
3. Build a row string
rows = 5
for row in range(1, rows + 1):
    line = "".join(str(value) for value in range(1, row + 1))
    print(line)
Separating construction of a line from printing it.
4. Use a comprehension
rows = 5
for row in range(1, rows + 1):
    print("".join([str(value) for value in range(1, row + 1)]))
A compact variation. The inner comprehension creates the values for one row; the outer loop prints each row.
5. Repeat the row number
rows = 5
for row in range(1, rows + 1):
    print(str(row) * row)
A related repeated-digit pattern, not the ascending sequence.

Methods 3 and 4 express the same row-by-row logic while constructing a string before printing. Method 5 changes the rule: it repeats each row number instead of counting from 1 within each row. A beginner-oriented nested-loop triangle also appears in Waterside Code Club’s Python From Zero To One.

Eight related pattern variations

Each variation changes a loop boundary, the value printed, or the spacing. Here, rows = 5 unless the example uses another setting.

1. Repeated digit on each row

Output:

1
22
333
4444
55555
for row in range(1, rows + 1):
    print(str(row) * row)

Multiplying the row’s string by its number repeats that digit; this is the repeated-digit pattern shown as method 5.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. Descending row lengths

Output:

12345
1234
123
12
1
for row in range(rows, 0, -1):
    for value in range(1, row + 1):
        print(value, end="")
    print()

The outer loop counts downward, so each new row is shorter.

3. Descending values within each row

Output:

1
21
321
4321
54321
for row in range(1, rows + 1):
    for value in range(row, 0, -1):
        print(value, end="")
    print()

The inner loop starts at the row number and counts down to 1.

4. Add spaces between ascending values

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
for row in range(1, rows + 1):
    print(" ".join(str(value) for value in range(1, row + 1)))

Joining the values with a space changes the separator, not the numbers in each row.

5. Print an asterisk triangle

Output:

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

This replaces the numbers with a repeated asterisk.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

6. Continue consecutive integers across rows

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
rows = 5
value = 1
for row in range(1, rows + 1):
    for _ in range(row):
        print(value, end=" " if _ < row - 1 else "")
        value += 1
    print()

Unlike the original pattern, this keeps one counter across rows rather than restarting each row at 1.

7. Center a number pyramid

Output:

    1
   1 2
  1 2 3
 1 2 3 4
1 2 3 4 5
rows = 5
for row in range(1, rows + 1):
    numbers = " ".join(str(value) for value in range(1, row + 1))
    print(" " * (rows - row) + numbers)

Leading spaces move shorter rows to the right. This is a simple text alignment; it is not a geometrically centered pyramid when the number of digits or spacing changes.

8. Ascending and then descending values

Output:

1
121
12321
1234321
123454321
for row in range(1, rows + 1):
    up = range(1, row + 1)
    down = range(row - 1, 0, -1)
    print("".join(str(value) for value in up), end="")
    print("".join(str(value) for value in down))

The descending part starts at row - 1 so the peak value is not printed twice.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common mistakes and fixes

  • Only one value appears per line: the inner print() is ending each line. Use end="" (or a chosen separator) while printing each value, then call print() after the inner loop.
  • The last value is missing: range() excludes its stop value. For an inclusive endpoint of row, use range(1, row + 1).
  • The while-loop never ends: increment value inside the inner loop and row after the row is printed. Reset value at the start of each row.
  • The fifth method does not print 123: str(row) * row intentionally repeats one digit, producing 333 on row 3. Use the nested loop or row-string method for 123.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.