• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
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.  Using Python to Sum Odd Numbers in List
  • 2.  Read First Line of File Using Python
  • 3.  Python sinh – Find Hyperbolic Sine of Number Using math.sinh()
  • 4.  Check if Class is Subclass in Python with issubclass()
  • 5.  Convert String to Integer with int() in Python
  • 6.  How to Shutdown Computer with Python
  • 7.  Time Difference in Seconds Between Datetimes in Python
  • 8.  Generate Random Float Between 0 and 1 Using Python
  • 9.  pandas dropna – Drop Rows or Columns with NaN in DataFrame
  • 10.  How to Check if Number is Negative in Python

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 *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

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