0
0
PythonHow-ToBeginner · 3 min read

How to Change Turtle Color in Python Easily

To change the turtle color in Python, use the color() method from the turtle module. You can pass a color name as a string like "red" or a color code like "#FF0000" to set the turtle's pen color.
📐

Syntax

The basic syntax to change the turtle's color is:

  • turtle.color(color_name): Sets the pen color for drawing.
  • color_name can be a string with a color name (e.g., "blue") or a hex color code (e.g., "#00FF00").
python
import turtle

t = turtle.Turtle()
t.color("blue")  # Set the turtle's pen color to blue
💻

Example

This example shows how to create a turtle, change its color to green, and draw a square.

python
import turtle

screen = turtle.Screen()
t = turtle.Turtle()

t.color("green")  # Change turtle color to green

for _ in range(4):
    t.forward(100)
    t.right(90)

screen.mainloop()
Output
A window opens showing a green square drawn by the turtle.
⚠️

Common Pitfalls

Common mistakes when changing turtle color include:

  • Using t.color without parentheses, which does nothing.
  • Passing invalid color names or codes, causing errors or default colors.
  • Confusing color() with fillcolor(), which sets the fill color for shapes.
python
import turtle

t = turtle.Turtle()

# Wrong: missing parentheses, no color change
# t.color "red"  # SyntaxError

# Correct:
t.color("red")  # Sets pen color to red

# Wrong: invalid color name
# t.color("reddish")  # Causes error or default color

# Correct:
t.color("red")
📊

Quick Reference

MethodPurposeExample
color()Sets the pen color for drawing linest.color("blue")
fillcolor()Sets the fill color for shapest.fillcolor("yellow")
pencolor()Alias for color(), sets pen colort.pencolor("#FF00FF")

Key Takeaways

Use the turtle's color() method with a color name or hex code to change its pen color.
Always include parentheses when calling color(), like t.color("red").
Invalid color names cause errors or fallback colors, so use standard color names or hex codes.
color() changes the pen color; use fillcolor() to change the fill color of shapes.
Test your color changes by drawing simple shapes to see the effect immediately.