• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Draw Star in Python Using turtle Module

Draw Star in Python Using turtle Module

July 25, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  Creating a List of Zeros in Python
  • 2.  How to Get the Size of a List in Python Using len() Function
  • 3.  Check if Variable is Integer in Python
  • 4.  Python isdigit() Function – Check if Characters in String are Digits
  • 5.  Mastering Data Transformation with Pandas Pivot Tables
  • 6.  Drop Duplicates pandas – Remove Duplicate Rows in DataFrame
  • 7.  How to Return Nothing in Python from Function
  • 8.  Calculate Compound Interest in Python
  • 9.  Python tan – Find Tangent of Number in Radians Using math.tan()
  • 10.  pandas Absolute Value – Get Absolute Values in a Series or DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · The Programming Expert · About · Privacy Policy