Skip to content

S3Q2 · Print Pattern - V Shape

⚡ Quick Reference

Type: Full I/O - pattern printing

Core idea: row i has \ at column i-1, inner spaces 2*(n-i)-1, then /. Last row has v at column n-1.

n = int(input())
for i in range(1, n + 1):
    if i == n:
        print(" " * (n - 1) + "v")
    else:
        inner = " " * (2 * (n - i) - 1)
        print(" " * (i - 1) + "\\" + inner + "/")

Key rules: - Row i (1-indexed): i-1 leading spaces, then \, inner spaces, then / - Inner spaces at row i: 2*(n-i) - 1 - Last row: n-1 leading spaces then v - No trailing spaces - the / or v is the last character


Problem Statement

Problem (I/O type)

Given n, print a V-shaped pattern. \ and / move diagonally inward each row, meeting at v on the last row.

Examples:

Input
3
Output
\   /
 \ /
  v
Input
2
Output
\ /
 v

Deriving the pattern

For n=4:

\     /      row 1: 0 leading, 5 inner spaces
 \   /       row 2: 1 leading, 3 inner spaces
  \ /        row 3: 2 leading, 1 inner space
   v         row 4: 3 leading, just "v"
Row i Leading spaces Inner spaces Characters
1 0 2*(4-1)-1 = 5 \·····/
2 1 2*(4-2)-1 = 3 ·\···/
3 2 2*(4-3)-1 = 1 ··\·/
4 3 - (last row) ···v

Formula for inner spaces at row i: 2*(n-i) - 1

At the last row (i = n): 2*(n-n) - 1 = -1 - not valid, so handle separately with just v.

Why 2*(n-i) - 1 inner spaces?

Each row, \ moves one column right and / moves one column left - together they close by 2 per row. Starting from 2*(n-1) - 1 spaces at row 1, the gap reduces by 2 each row until it's 1 (row n-1), then disappears at v.


Tracing n = 3

i Leading Inner Row printed
1 0 3 \···/
2 1 1 ·\/·\ /? Wait - inner=1 → \ /
3 2 - ··v

For i=2: leading=1, inner=2*(3-2)-1=1, row = · + \ + + / = ·\ / = \ /


Solution approaches

n = int(input())
for i in range(1, n + 1):
    if i == n:
        print(" " * (n - 1) + "v")
    else:
        print(" " * (i - 1) + "\\" + " " * (2 * (n - i) - 1) + "/")
n = int(input())
for i in range(1, n + 1):
    if i == n:
        # last row - just v
        row = " " * (n - 1) + "v"
    else:
        leading = " " * (i - 1)
        inner   = " " * (2 * (n - i) - 1)
        row = leading + "\\" + inner + "/"
    print(row)
n = int(input())
make_row = lambda i: (
    " " * (n - 1) + "v"
    if i == n else
    " " * (i - 1) + "\\" + " " * (2 * (n - i) - 1) + "/"
)
for i in range(1, n + 1):
    print(make_row(i))

Edge case: n = 1

i=1 is also i=n, so the only row is v with 0 leading spaces → "v"


Key takeaways

01

2*(n-i) - 1 inner spaces

The V closes by 2 spaces each row - \ moves right, / moves left. Starting at 2*(n-1)-1, reducing by 2 each step until 1, then the last row becomes v.

02

Escape backslash as \\

\ is a Python escape character. Always write "\\" to print a literal backslash. In f-strings, place "\\" outside the braces or use a variable.

03

Handle the last row separately

The last row breaks the general formula (inner spaces would be -1). Use if i == n to print v before applying the formula to all other rows.