square()
The function square() can be used to generate square patterns using characters or numbers
Function Signature
square(
n: int,
char: str = "*",
numeric: bool = False,
increment: bool = False,
space: bool = False
) -> str
square() creates an n * n grid based on configuration options:
- Character-based patterns (default)
- Numeric patterns
- Incremental sequences
- Optional spacing for readabiility
Parameter
| Name | Type | Default | Description |
|---|---|---|---|
n |
int | — | Size of the square (rows and columns). Must be positive. |
char |
str | "*" |
Character used to build the square. Must be a single character. |
numeric |
bool | False |
If True, generates numeric patterns instead of characters. |
increment |
bool | False |
If True, rows contain 1 → n instead of repeating numbers. Requires numeric=True. |
space |
bool | False |
Adds spaces between elements for better readability. |
Returns
- str — A string representing the square pattern with newline-separated rows
Raises
| Exception | Condition |
|---|---|
ValueError |
If n <= 0 |
ValueError |
If char is not a single character |
ValueError |
If numeric=True and char is provided |
ValueError |
If increment=True but numeric=False |
Basic Usage
Charater Mode (default)
Repeats the given character across all rows.
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Usage Examples
1. Default Square
print(square(3))
***
***
***
2. Default Square (with spaces)
print(square(3, space=True))
* * *
* * *
* * *
Numeric Mode
- Each row uses its row number:
1 1 1
2 2 2
3 3 3
Usage Examples
1. Simple Numeric Pattern
from patlab import square
print(square(4, numeric=True))
1111
2222
3333
4444
2. Simple Numeric Pattern (with spaces)
from patlab import square
print(square(4, numeric=True, space=True))
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
3. Simple Incremental Numeric Pattern
from patlab import square
print(square(4, numeric=True, incremental=True))
1234
1234
1234
1234
4. Simple Incremental Numeric Pattern (with spaces)
from patlab import square
print(square(4, numeric=True, incremental=True, spaces=True))
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Custom Character Mode
Repeats the given custom character across all rows.
Usage Examples
print(square(4, "#"))
####
####
####
####
print(square(3, "1"))
111
111
111
print(square(4,"#",space=True))
# # # #
# # # #
# # # #
# # # #
print(square(3, "1", space=True))
1 1 1
1 1 1
1 1 1
Notes
charris ignored whennumeric=Trueincremental=Trueonly works whennumeric=True- Output is always returned as a string (not printed)
Tips
- Combine options carefully to avoid
ValueError