To draw a star in Python, we can use the Python turtle module.

import turtle

t = turtle.Turtle()

def draw_star(size):
    for i in range(0,5):
        t.forward(size)
        t.right(144)

draw_star(100)

The turtle module in Python allows us to create graphics easily in our Python code.

We can use the turtle module to make all sorts of shapes in Python. For example, we can draw circles and draw rectangles easily in Python with the turtle module.

One other shape which is easy to make is a star.

Stars typically are made up of 5 points. To draw a star in Python, we need to have our turtle draw the five lines.

We can create a simple star by defining a function that takes in an integer representing the length of each line. Then we can loop five times, using the forward() function to create the side, and then rotating the cursor 144 degrees with the right() function.

Below is a simple example of how to use Python to make a star.

import turtle

t = turtle.Turtle()

def draw_star(size):
    for i in range(0,5):
        t.forward(size)
        t.right(144)

draw_star(100)

python turtle star

How to Draw a Star with Different Colors in Python

With turtle colors in Python, we can change the colors of our shapes easily.

The main function you can use to change the color of a line is with the turtle pencolor() function.

Below is an example and the output of how to draw a green star using pencolor() in Python.

import turtle

t = turtle.Turtle()

t.pensize(4)

t.pencolor("green")

def draw_star(size):
    for i in range(0,5):
        t.forward(size)
        t.right(144)

draw_star(100)

turtle green star

With turtle, you can also easily fill shapes with color.

To fill a shape, there are a few steps to take. We use the fillcolor() function to define the fill color of our shape, and then use the begin_fill() and end_fill() functions to define when to begin and end filling shapes with the fill color.

Just like the pencolor() function, the fillcolor() function takes any valid color given a color mode.

Let’s take the example from above and fill our star with the color ‘light blue’ using fillcolor(), begin_fill() and end_fill() in Python.

import turtle

t = turtle.Turtle()

t.fillcolor("light blue")

t.pensize(4)

t.pencolor("green")

t.begin_fill()

def draw_star(size):
    for i in range(0,5):
        t.forward(size)
        t.right(144)

draw_star(100)

t.end_fill()

turtle light blue fill star green

Hopefully this article has been helpful for you to learn how to draw a star in Python.

Categorized in:

Python,

Last Update: March 1, 2024