Right Triangle Star Pattern in Python — Beginner Guide

· 3 min read

⚡ TL;DR

Learn how to print a right triangle star pattern in Python with for-loops. Includes full code, output walkthrough, and 5 variations for beginners.

The right triangle star pattern is the most common first pattern in programming tutorials. It teaches you nested loops, printing without newline, and how to control row-by-row output.

What We’ll Build

Copy
*
* *
* * *
* * * *
* * * * *

Basic Approach: Nested For Loops

The outer loop controls the row, inner loop controls how many stars per row.

Copy
def right_triangle(n):
    for i in range(1, n + 1):  # rows
        for j in range(i):      # stars per row
            print("*", end=" ")
        print()                 # new line after each row

# Test with 5 rows
right_triangle(5)

Output

Copy
* 
* * 
* * * 
* * * * 
* * * * * 

Variation 1: Using end and sep

Python 3 lets you control line endings directly.

Copy
def right_triangle_v2(n):
    for i in range(1, n + 1):
        print("* " * i, end="")
        print()  # newline

right_triangle_v2(5)

Output

Copy
*
* *
* * *
* * * *
* * * * *

Variation 2: Using str.join()

Copy
def right_triangle_v3(n):
    for i in range(1, n + 1):
        print(" ".join(["*"] * i))

right_triangle_v3(5)

Output

Copy
*
* *
* * *
* * * *
* * * * *

Variation 3: With Numbers Instead of Stars

Copy
def right_triangle_numbers(n):
    for i in range(1, n + 1):
        print(" ".join(str(j) for j in range(1, i + 1)))

right_triangle_numbers(5)

Output

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

Variation 4: Right-Aligned (Left Triangle)

Copy
def left_triangle(n):
    for i in range(1, n + 1):
        print(" " * (n - i) + "* " * i, end="")
        print()

left_triangle(5)

Output

Copy
    * 
   * * 
  * * * 
 * * * * 
* * * * * 

Common Beginner Errors

  1. Forgetting end="" → every star prints on its own line
  2. Off-by-one in range → Python’s range(1, n+1) is exclusive, so you get exactly n rows
  3. Mixing tabs and spaces → inconsistent alignment

Time & Space Complexity

MetricValue
TimeO(n²)
SpaceO(1) (no extra storage)

Practice Exercises

  1. Change the star count formula: try print("* " * (2*i - 1)) to get odd columns
  2. Print row numbers instead of stars: 1, 2 2, 3 3 3
  3. Print reversed rows: 5 4 3 2 1, 4 3 2 1, etc.

Try these variations in your own Python REPL or script. Next up: Inverted Right Triangle Pattern in Python

FAQ

How do you print the Right Triangle Star pattern in Python?

Use nested loops: the outer loop walks through the rows while the inner loop prints the characters or values for each row. The complete Python implementation with expected output is shown in the sections above.

What is the time complexity of the Right Triangle Star pattern in Python?

The time complexity is O(n²) and the space complexity is O(1), since the pattern is built with a fixed number of loop counters and printed row by row.

Related Blogs
Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlocking Flutter's Potential: Best Practices for Writing Clean Code

Unlock Flutter's potential with clean code. Learn best practices for writing maintainable Dart code, with examples and essential principles.

CLEAN CODECODE BEST PRACTICESCODE MAINTAINABILITYCODE ORGANIZATIONCODE READABILITY

June 02, 2023

Top 9 Local Databases for Flutter: Full Comparison

Top 9 Local Databases for Flutter: Full Comparison

Compare the best local databases for Flutter (Isar, Hive, Sqflite, ObjectBox, Realm, Drift, Floor, CBL, Sembast). Find pros, cons, and performance comparisons.

CODINGFLUTTERFLUTTER LOCAL DATABASESLEARN TO CODENOSQL

April 06, 2023

Related Tutorials
Essential Git Skills: How to Delete Remote Branches

Essential Git Skills: How to Delete Remote Branches

Keep your repository clean and organized. This guide walks you through how to delete both local and remote branches in Git, step by step.

BITBUCKETDELETE LOCAL BRANCHDELETE REMOTE BRANCHGITGIT BRANCH CLEANUP AUTOMATION

June 12, 2024

Show an Offline Message for No Internet in Flutter

Show an Offline Message for No Internet in Flutter

You have to use the 'Connectivity Flutter Package' to achieve this feature on your App. This package helps to know whether your device is online or offline.

CONNECTIVITY PLUSDEPENDENCIESFLUTTERFLUTTER DEVELOPMENTFLUTTER PACKAGES

April 09, 2024

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Mastering TabBar & TabBarView in Flutter: A Complete Guide

Implement TabBar and TabBarView in Flutter step by step — customize tab indicators, enable scrollable tabs, and change tabs programmatically.

CODINGDEFAULT TAB CONTROLLERFLUTTERLEARN TO CODETAB CONTROLLER

July 26, 2023

File Manipulation in Flutter: Best Practices & Examples

File Manipulation in Flutter: Best Practices & Examples

Master Flutter file manipulation — permission management, directory handling, and practical read-write scenarios to elevate your app's file handling.

CODINGFLUTTERFLUTTER DEVELOPMENTFLUTTER FILE OPERATIONFLUTTER PATH PROVIDER

June 30, 2023

Related Recommended Services
Visual Studio Code for the Web

Visual Studio Code for the Web

Build with Visual Studio Code, anywhere, anytime, in your browser.

IDEVISUAL STUDIOVISUAL STUDIO CODEWEB
Renovate | Automated Dependency Updates

Renovate | Automated Dependency Updates

Renovate Bot keeps source code dependencies up-to-date using automated Pull Requests.

AUTOMATED DEPENDENCY UPDATESBUNDLERCOMPOSERGITHUBGO MODULES
Best XML Formatter and XML Beautifier

Best XML Formatter and XML Beautifier

Online XML Formatter will format xml data, helps to validate, and works as XML Converter. Save and Share XML.

XMLXML BEAUTIFIERXML CONVERTERXML FORMATXML FORMATTER
Kubecost | Kubernetes cost monitoring and management

Kubecost | Kubernetes cost monitoring and management

Kubecost started in early 2019 as an open-source tool to give developers visibility into Kubernetes spend. We maintain a deep commitment to building and supporting dedicated solutions for the open source community.

CLOUDKUBECOSTKUBERNETESOPEN SOURCESELF HOSTED
Related Recommended Stories
How GitHub reduced testing time for iOS apps with new runner features

How GitHub reduced testing time for iOS apps with new runner features

Learn how GitHub used macOS and Apple Silicon runners for GitHub Actions to build, test, and deploy our iOS app faster.

IOSGITHUBTESTINGRUNNER
5 ways to transform your workflow using GitHub Copilot and MCP

5 ways to transform your workflow using GitHub Copilot and MCP

Learn how to streamline your development workflow with five different MCP use cases.

AGENT MODECODING AGENTCOPILOTFIGMAGITHUB
One weird trick for powerful Git aliases

One weird trick for powerful Git aliases

Advanced Git Aliases

ALIASALIAS TEMPLATEATLASSIANBITBUCKETGIT
Awesome Python

Awesome Python

An opinionated list of awesome Python frameworks, libraries, software and resources

AWESOMEAWESOME PYTHONCOLLECTIONSGITHUBPYTHON
Related Recommended Tools
Find out what websites are built with - Wappalyzer

Find out what websites are built with - Wappalyzer

Find out the technology stack of any website. Create lists of websites and contacts by the technologies they use.

ADD ONSANALYTICSAPP STOREAPPLEBOOKING
Sourcetree | Free Git GUI for Mac and Windows

Sourcetree | Free Git GUI for Mac and Windows

A Git GUI that offers a visual representation of your repositories. Sourcetree is a free Git client for Windows and Mac.

GITGITHUBGITLABATLASSIANBITBUCKET
Related Recommended Videos