How to Print the 1 12 123 Pattern in Python

Recently, while teaching a Python basics class, one of my students asked me how to print a simple number pattern like 1, 12, 123, and so on. It reminded me of my early coding days when I used to practice Python pattern programs to strengthen my logic and control flow skills.

In this tutorial, I’ll show you how to print the 1 12 123 pattern in Python using a few different methods.

We’ll start with the simplest approach using a for loop, then explore a while loop, and finally, I’ll share a slightly advanced version using string concatenation.

Learn to Print Number Patterns in Python

Printing number patterns in Python might seem basic, but it’s one of the most effective ways to improve logical thinking. It helps you understand loop nesting, range functions, and string formatting, which are all essential parts of Python programming.

Even if you’re working on data analysis or automation in the USA, understanding loops and control structures can help you write more efficient and readable code.

Method 1 – Use a For Loop in Python

The easiest way to print the 1 12 123 pattern in Python is by using a for loop. This method is simple, beginner-friendly, and helps you visualize how nested loops work together.

Here’s the complete Python code:

rows = 5

# Outer loop for each row
for i in range(1, rows + 1):
    # Inner loop to print numbers in each row
    for j in range(1, i + 1):
        print(j, end="")
    # Move to the next line after each row
    print()

When you run this code, the output will be:

1
12
123
1234
12345

You can refer to the screenshot below to see the output.

1 12 123 pattern in python

This approach uses two loops, one to handle the number of rows and one to print numbers in each row. The end=”” parameter ensures that the numbers are printed on the same line without spaces.

How This Python Code Works

In the above Python code, the outer loop (for i in range(1, rows + 1)) controls how many rows are printed. The inner loop (for j in range(1, i + 1)) prints numbers from 1 up to the current row number.

For example:

  • When i = 1, the inner loop prints 1
  • When i = 2, it prints 12
  • When i = 3, it prints 123

By the time the loop finishes, you’ll have a neat triangular pattern that displays the 1 12 123 sequence.

Method 2 – Use a While Loop in Python

If you prefer using a while loop, you can easily achieve the same pattern. This method gives you more control over the loop variables and might be easier to understand for those transitioning from other languages like C or Java.

Here’s the Python code using a while loop:

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

You can refer to the screenshot below to see the output.

how to print 1 12 123 in python

This code produces the same result as the for loop version but uses explicit loop counters.
It’s a great way to understand how loop variables are incremented manually in Python.

Why I Like the While Loop Method

I often use the while loop approach when I need more flexibility in controlling loop conditions. In real-world Python projects, this kind of control can be useful when dealing with dynamic data or user input.

For example, you could easily modify the above code to take the number of rows from user input:

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

Now, the program becomes interactive and can adapt to different user needs, a common scenario in Python automation scripts.

Method 3 – Use String Concatenation in Python

Another interesting way to print the 1 12 123 pattern in Python is by using string concatenation. Instead of printing numbers one by one, we build the entire line as a string and print it once per row.

Here’s how you can do it:

rows = 5
pattern = ""

for i in range(1, rows + 1):
    pattern += str(i)
    print(pattern)

Output:

1
12
123
1234
12345

You can refer to the screenshot below to see the output.

print 1 12 123 in python

This method is elegant and very Pythonic. It avoids nested loops and instead uses string operations to build each line progressively.

How This Method Works

In this version, we start with an empty string pattern. Each time the loop runs, we append the next number (str(i)) to the string and print it.

This approach is efficient for small patterns and demonstrates how Python handles string concatenation dynamically. However, for larger datasets, it’s better to use loops or string join operations for better performance.

Method 4 – Print the Pattern in Reverse Order

Once you understand how to print the pattern in ascending order, it’s fun to reverse it. This helps you practice nested loops and conditional logic in Python.

Here’s the Python code:

rows = 5

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

Output:

12345
1234
123
12
1

This version uses a reverse range in the outer loop. It’s a great exercise to understand how Python handles descending sequences with the range() function.

Bonus – Print the Pattern Side by Side

Sometimes, you may want to display both ascending and descending patterns together. This is a slightly advanced version, but a great way to test your Python skills.

rows = 5

for i in range(1, rows + 1):
    # Ascending part
    for j in range(1, i + 1):
        print(j, end="")
    # Space between patterns
    print("  ", end="")
    # Descending part
    for j in range(i, 0, -1):
        print(j, end="")
    print()

Output:

1  1
12  21
123  321
1234  4321
12345  54321

This pattern looks visually appealing and shows how loops can be combined creatively in Python. It’s also a great addition to your coding portfolio if you’re preparing for interviews or teaching Python basics.

Common Mistakes to Avoid

When printing number patterns in Python, beginners often make a few common mistakes:

  • Forgetting to reset variables like pattern inside loops.
  • Missing the end=”” parameter, which causes unwanted new lines.
  • Using print() incorrectly inside nested loops.

Always test your code after small changes and use comments to explain your logic.
This habit will make your Python code cleaner and easier to debug.

Practical Use Cases of Pattern Printing in Python

While pattern printing might seem like a simple exercise, it builds a strong foundation for:

  • Data formatting in reports or logs.
  • Generating structured output for console-based tools.
  • Improving problem-solving skills for coding interviews.

In the USA, many Python developers use similar logic for generating structured data outputs in automation scripts and testing tools. So, practicing these small patterns today can help you write more efficient scripts tomorrow.

Conclusion

So, that’s how you can print the 1 12 123 pattern in Python using different methods, for loops, while loops, and even string concatenation. Each approach helps you understand a different aspect of Python programming, from loops to string handling.

I always recommend beginners start with pattern-based problems because they sharpen your logical thinking and improve your understanding of Python loops. Once you master these, you’ll find it easier to write more complex programs and automation scripts.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.