0
0
PythonHow-ToBeginner · 4 min read

How to Draw Flag Using Turtle in Python - Simple Guide

Use Python's turtle module to draw a flag by creating colored rectangles or shapes representing the flag's design. You set the turtle's position, choose colors with fillcolor(), and use begin_fill() and end_fill() to fill shapes with color.
📐

Syntax

To draw shapes with the turtle module, you use commands to move the turtle and fill colors. Key commands include:

  • turtle.fillcolor(color): sets the fill color.
  • turtle.begin_fill(): starts filling the shape.
  • turtle.end_fill(): ends filling the shape.
  • turtle.forward(distance): moves the turtle forward.
  • turtle.right(angle) or turtle.left(angle): turns the turtle.
  • turtle.penup() and turtle.pendown(): lift or place the pen to move without drawing.
python
import turtle

t = turtle.Turtle()
t.fillcolor('red')
t.begin_fill()
for _ in range(4):
    t.forward(100)
    t.right(90)
t.end_fill()
turtle.done()
Output
A red square is drawn on the screen.
💻

Example

This example draws a simple flag with three horizontal stripes: red, white, and blue. It shows how to position the turtle and fill rectangles with colors.

python
import turtle

screen = turtle.Screen()
t = turtle.Turtle()
t.speed(1)

stripe_height = 40
stripe_width = 200

colors = ['red', 'white', 'blue']

# Start at top-left corner
start_x = -stripe_width / 2
start_y = stripe_height * len(colors) / 2

t.penup()
t.goto(start_x, start_y)
t.pendown()

for color in colors:
    t.fillcolor(color)
    t.begin_fill()
    for _ in range(2):
        t.forward(stripe_width)
        t.right(90)
        t.forward(stripe_height)
        t.right(90)
    t.end_fill()
    t.right(90)
    t.penup()
    t.forward(stripe_height)
    t.left(90)
    t.pendown()

# Hide turtle and finish
 t.hideturtle()
turtle.done()
Output
A flag with three horizontal stripes colored red, white, and blue is drawn.
⚠️

Common Pitfalls

Common mistakes when drawing flags with turtle include:

  • Not lifting the pen (penup()) when moving to a new position, causing unwanted lines.
  • Forgetting to call begin_fill() and end_fill(), so shapes are not filled with color.
  • Incorrect turtle orientation causing shapes to draw in wrong directions.
  • Not setting the turtle speed, making drawing slow.
python
import turtle

t = turtle.Turtle()

# Wrong: moving without penup causes lines
# t.forward(100)
# t.right(90)
# t.forward(50)

# Right way:
t.penup()
t.forward(100)
t.right(90)
t.forward(50)
t.pendown()
Output
No drawing while moving with penup; drawing resumes after pendown.
📊

Quick Reference

CommandDescription
fillcolor(color)Set the fill color for shapes
begin_fill()Start filling the shape with color
end_fill()Stop filling the shape
forward(distance)Move turtle forward by distance
right(angle)Turn turtle right by angle degrees
left(angle)Turn turtle left by angle degrees
penup()Lift pen to move without drawing
pendown()Place pen down to draw

Key Takeaways

Use turtle's fillcolor, begin_fill, and end_fill to create colored shapes for flags.
Lift the pen with penup before moving to avoid unwanted lines.
Set turtle speed to control drawing speed for better visuals.
Position the turtle carefully to draw flag parts in correct places.
Use loops to draw repeated flag stripes or shapes efficiently.