How to Draw a Star in Python (Using Turtle): A Step-by-Step Guide

The Turtle module in Python is a library that allows users to create simple graphics programs using a turtle. This guide teaches you how to use Turtle to draw a star with 5 edges as well as 10 edges.

In Python, Turtle is an object that can be moved around the screen, and it can be given commands to draw lines, change its color, and more. By moving the Turtle object in a specific way with a specific angle, you can generate a star-like shape.

Let’s jump into it!

5 Steps to Draw a Star Using Python Turtle

To draw a star with Python Turtle, you can use the following steps:

  1. Import the turtle library in your Python code. This will give you access to the functions and methods you need to create turtle graphics.
import turtle
  1. Create a new Turtle object. This will be the turtle that you use to draw the star.
t = turtle.Turtle()
  1. Use a for loop to repeat a sequence of steps that will draw the star. In each iteration of the loop, use the t.forward() method to move the turtle forward by a specified distance, and the t.right() method to turn the turtle to the right by a certain number of degrees.
for i in range(5):
    t.forward(100)
    t.right(144)

This code draws a line 100 pixels forward and then turns 144 degrees towards the left. After doing this five times, you end up turning the turtle around by 5 * 144 = 720 degrees to stop it at the same place it started forming a star shape.

If you run this code, you should see a star with 5 edges:

Python star with 5 edges using turtle
  1. To make the star have more edges, tweak the angle that the turtle turns after each forward movement. Instead of turning by 144 degrees, turn 108 degrees. This makes the turtle’s turns take place at less steep angles which means it will complete the star shape with more strokes. If you’re using 108 degrees as the angle of your star, it requires 1080 degrees of rotation to get the Turtle back to its starting position.
for i in range(10):
    t.forward(100)
    t.right(108)

Running this piece of code gives you a star that looks like this:

Python star with 10 edges using Turtle

Full Code

Here’s the complete code to draw a star with 5 edges in Python Turtle:

import turtle

t = turtle.Turtle()

for i in range(5):
    t.forward(100)
    t.right(144 - 36)

And here’s the code to draw a star with 10 edges:

import turtle

t = turtle.Turtle()

for i in range(10):
    t.forward(100)
    t.right(108)

How to Color the Star?

If you want to add color to the star in Turtle, you can use the fillcolor() method to shade the area restricted by the Turtle strokes.

Here’s what it looks like in the code:

import turtle

my_turtle = turtle.Turtle()

# Set the turtle's pen color
my_turtle.pencolor("black")

# Set the star's fill color
my_turtle.fillcolor("yellow")

# Begin filling the star
my_turtle.begin_fill()

# Draw the star
for i in range(5):
    my_turtle.forward(100)
    my_turtle.left(144)

# End filling the star
my_turtle.end_fill()

# Keep the turtle window open
turtle.done()

Output:

Python star drawn with Turtle

Notice how the edge that points toward the top left looks out of place because there’s no stroke that would strike through the edge similar to the other edges. To overcome this issue, repeat the first stroke by adding one more iteration to the for loop:

import turtle

my_turtle = turtle.Turtle()

# Set the turtle's pen color
my_turtle.pencolor("black")

# Set the star's fill color
my_turtle.fillcolor("yellow")

# Begin filling the star
my_turtle.begin_fill()

# Draw the star
for i in range(6):
    my_turtle.forward(100)
    my_turtle.left(144)

# End filling the star
my_turtle.end_fill()

# Keep the turtle window open
turtle.done()

Output:

Summary

Today you learned how to use Turtle to draw a star in Python.

To recap, Python Turtle is a module that allows users to create simple graphics using turtle graphics in the Python programming language. It is commonly used to introduce beginners to the basics of programming and creating simple shapes and images.

The Turtle can be moved and instructed to draw lines, shapes, and other designs, making it a useful tool for creating interactive and visually appealing programs.

One great example is drawing a star with it which is what you learned today.

Thanks for reading. Happy coding!

Read Also

How to Draw a Christmas Tree in Python