Python Program to Print a Staircase Pattern
Beginner · Patterns
A staircase pattern prints steps of `#` that rise from left to right: the bottom step is n characters wide, each step above is one shorter, and all of them line up on the right. It is a popular coding practice problem, usually asked as a function that takes the number of steps n.
Program
A staircase of 5 steps:
#
##
###
####
#####How it works
- The drawing is done by a function,
staircase(n), so the same code can draw a staircase of any size. - Row
ihasn - ispaces followed byihashes, so every row is exactlyncharacters wide. - Because every row has the same width, the hashes all end in the last column and the steps rise from left to right.
- The top row has a single
#and the bottom row is a full row ofnhashes.
Other ways to write it
Wider steps
###
######
#########
############
###############Multiplying by width makes every step 3 characters wide, which looks more like a real flight of stairs.
Stairs drawn with lines
___|
___|
___|
___|
___|Each step is a tread ___ and a riser |, and every step up starts 4 characters further right, so the steps join into one flight of stairs.
Try it yourself
Change staircase() so the steps go down from left to right instead.
Edit the program above and press Run, or open it in the full editor to save it and share it. To watch it run line by line, open it in the visualizer.
Related programs
All Python programs · CBSE class 12 practical file · Python exercises