Python Drawing with Turtle: Commands for Beginners

Python turtle drawing with a labeled square

Python drawing is straightforward with the standard turtle module. This example uses Python turtle graphics to draw a blue square, move without leaving a line, and add a centered label.

The complete example also shows how to draw in Python with movement, turns, pen controls, a loop, coordinates, color, line width, and turtle.write.

Python Drawing: Create a Turtle Screen and Pen

Import the built-in module first. Then create a screen and a turtle object that acts as your drawing pen:

import turtle
screen = turtle.Screen()
pen = turtle.Turtle()

screen is the drawing window, while pen stores the turtle’s position, direction, color, and pen state. A new turtle starts at coordinate (0, 0), usually facing right. The screen uses ordinary x and y coordinates: positive x moves right, and positive y moves up.

Use Python turtle commands to Move, Turn, and Repeat a Shape

The main Python turtle commands control distance and direction. pen.forward(120) moves 120 pixels in the current direction and draws a line. pen.backward(40) moves backward while drawing. pen.left(90) and pen.right(90) turn the turtle by 90 degrees without moving it.

A square needs four equal sides and four 90-degree turns. A for loop repeats those two actions:

for _ in range(4):
    pen.forward(120)
    pen.left(90)

The underscore means that the loop count itself is not needed. Each repetition draws one side and turns the turtle for the next side. When the loop ends, the turtle returns to its starting coordinate and direction.

How to draw in Python: Control Position, Color, Width, and Pen State

Use pen.penup() to move without drawing and pen.pendown() to resume drawing. This prevents an unwanted line when moving from the completed square to its label.

pen.goto(60, 60) moves directly to a specific coordinate. With the pen raised, it creates no line. Use pen.color(“royalblue”) to set the drawing color and pen.width(3) to make the outline three pixels wide. Call these methods before the action they should affect.

For example, set the square’s color and width before the loop, then raise the pen before using goto(). If a diagonal line appears between the square and the label, penup() was omitted or called too late.

Add a Label with turtle.write and Keep the Window Open

Run these lines in order. The two indented lines belong inside the loop:

  1. import turtle
  2. screen = turtle.Screen()
  3. pen = turtle.Turtle()
  4. pen.shape(“turtle”)
  5. pen.color(“royalblue”)
  6. pen.width(3)
  7. for _ in range(4):
    • pen.forward(120)
    • pen.left(90)
  8. pen.penup()
  9. pen.goto(60, 60)
  10. pen.pendown()
  11. pen.color(“darkgreen”)
  12. turtle.write(“Square”, align=”center”, font=(“Arial”, 16, “bold”))
  13. screen.mainloop()

turtle.write() places text at the turtle’s current position. Here, align=”center” centers “Square” at coordinate (60, 60), while the font setting specifies the typeface, size, and bold style. The module-level function writes at the active turtle’s position; pen.write() is the equivalent object-based form.

screen.mainloop() keeps the turtle window open after the drawing finishes. The finished result is a 120-by-120 blue square with a green label centered inside it.