Imagine you have taken your first coding class in Python, and have learned the print() command. Maybe your teacher repeatedly shared the pattern: “ACTION WORD. Parentheses. Information!” to help you learn the pattern of how Python functions look. And you wonder: "Do Python messages always have to look simple and plain? Does coding have to be routine? "

Well, we are here to tell you…no! Python is awesome, because of the amazing community around it! People around the world build cool packages, and you have the power at your fingertips to use their code and create with it. And today we will be able to experience that. Python is one of the most versatile languages, used to process data or create video games.

So let’s dive right into learning how to make awesome Python turtle art!

Artist: Joan Stark

To enjoy live expert guidance learning Python, enroll your student our online Python For AI class and award-winning coding classes for teens designed by professionals from Google, Stanford, and MIT.

Let's Make Python Turtle Art

Follow along with the video tutorial here if you prefer:

1. Pick a code platform

Option A: Today, you can choose to simply use Replit to follow along – simply create a free account here and log in! Click on Create Repl. Select Python, name your code, and click Create Repl!

Option B: If you want to try out a ‘notebook’ environment for coding you can use a Jupyter Notebook: simply download the Anaconda Distribution from here. Then, open up the Anaconda Navigator app on your computer! Click on the Launch button under Jupyter Notebook. Click on New > Python 3 (ipykernel). Rename your notebook, and finally make sure to save as you code today.

Option C: If you like Google Drive, you can follow along using Google Colab. You need a Gmail account, and can simply login here. Click on File > New notebook. Rename your notebook, and make sure to save as you code today!

Tip: If you'd like expert-guided help setting up your coding environment and learning Python fast, explore our live, fun Python classes for kids with expert mentoring.

2. Install your basics

If you are using Google Colab, or Jupyter Notebook, make sure you install these 2 special tools today. It’s really easy. Simply type !pip install art and !pip install cowsay (fun name, right?). Remember, if you are using Repl.it no need to do the below.

Install basics for Python coding

Now, for the steps below, all the following code will be identical no matter which code platform you are using. So let’s begin!

3. Import, import!

Today, we will need to import the time, random, and art Python package. Simply use the import command.

Use the import command in Python coding

4. File away!

  1. Explore this amazing ASCII Art Archive Animals section to find some incredible designs!
  2. Choose one of your favorite animal drawings. Then simply highlight the drawing text, and copy it using Control-C or Command-C.
  3. On your computer, open up an app that allows you to store simple .txt files. You can use TextEdit (make sure to set it to Plain Text mode) on a Mac, or Notepad on Windows!
  4. Think of a label for the art you chose.
  5. Now, write your label adding special characters before it. We will be using ####. Here is an example: ####cat on a fence
  6. Hit enter.
  7. Now paste the art content using Control-V or Command-V, so it is directly underneath the label.
Cat on a fence
  1. Save your text file, naming it art.txt.
  2. Repeat the steps above 9 more times so you have a total of 10 favorite images added to art.txt. Make sure each one starts with its own ####<label> identifier on a new line!
  3. Add the file to the code space you are using (upload it to your Replit files, Jupyter directory, or Colab session).
Comic by Bizaaro Comics

5. Use the open command in Python

Great job! What we want to do is somehow store the art from art.txt in Python. Remember we labeled every ASCII drawing? Well, this means we created pairs of information: labels, and the ASCII drawings! Do you remember what works super well with pairs of information in Python? **hint: Dictionaries! **

So we need to do the following: magically teleport all of the contents from art.txt, and store it in a Python dictionary.

Turns out ‘magical teleportation’ in Python with files, it’s actually really easy. We will use the open() command. Then, to set up the teleportation we will create an empty dictionary, and an empty list.

Simply add the following code to your code file:

Open code in Python

6. Secret patterns

Now, we will go line by line of the text file. For this we can use a for loop. A for loop let's us repeat an action multiple times.

Then we will use the .split() command to use the secret pattern we set up so Python can tell the difference from a label, and a line for the ASCII art, and separate it.

.split() returns a List, and takes a String as input. But wait…what?! Secret pattern?

Remember, it was when we labeled every image using ####<label>. When Python goes line by line in the file, all it has to look for is the 4 hashtags #### to know that ‘Oh! This is a label, and not art’. This is what helps us separate our images.

Think of it like the bright yellow lines on the crosswalk on a busy street light. Just like how the lines help us know where we can safely cross the street, the #### help Python not get lost. Every time Python detects #### it knows to store the label. We’ll store the label in the list and dictionary we made.

When Python does not detect #### in the current line, it has to store the current art, linked to the previous label we just wrote down! For this we will simply retrieve the last label we added to the list. Did you know that a position of -1 means the end of a list?

Here is an example below. What will print(x[-1]) give us if x is the list below?

x = [‘ice cream’, ‘cookies’, ‘marshmallows’]

print(x[-1]) will print out…you guessed it! marshmallows!

Snip snip! ✂

Suppose we have a String variable x that holds the value ‘####cat on a fence\n’

x=‘####cat on a fence\n’

y = x.split(‘####’) will give us a List y, as if someone took scissors and cut up the String into chunks, cutting wherever there is a ####. Notice below, how the art label ends up being the second item of the list! This means we need to access position 1 of List y to store the art label. So, if we use split on our labels, the actual label is going to be in position 1. Remember, Python starts counting from zero.

Python

7. Invisible ink

Finally, notice how there was an ‘\n’ above. What does this mean? Well this is the computer’s way of saying a ‘new line’. There are special characters that act as "invisible ink". Imagine as if invisible ink was added every time we hit the Enter key in the .txt file. ‘\n’ is the invisible ink. And we want to get rid of this extra ink. We will use the .strip() command!

Phew! All these functions! .split(), .strip(), and we even have to remember to extract position 1 of the list! Can we avoid writing lots of lines of code you may wonder… Well, if you have a sequential step by step idea of multiple commands, you can simply use this pattern:

If command1() returns a List, and we want the 2nd position in the List to be provided as input to command2(), easy!

And for our situation, we might want something like this:

8. Bring it all together

In the steps above we didn’t code – why not? Why are they labeled as steps if we didn’t write any code? Well, what we did is think through and mentally map out steps we want to do. This is called pseudocode, and is one of the fundamental secrets of success for coding. Thinking through what you are going to do and how it may work, is extremely beneficial to you and your code.

Once you know what tools to use, why you want to use them, and their purpose, you are ready to code!

Below, we simply apply all the knowledge we learned above, to write the following code. Notice how we have added lots of comments. This is good practice, always add notes and explanations to your code so you are clear about what is going on, and remember it later. It's also helpful for any others that may read your code and want to understand your logic and decision making.

9. Practice with TIME, Text2ART & COWSAY!

Now, let’s interact with the user, the person who will run our code (which may be your or pretend it's someone else!), and ask them which animal ASCII art drawing they would like to see. We could just use print() and input() to do this. But why stick to simple, plain text? Why not bubble letters, fun fonts, and cool characters that say the messages instead?

  1. Choose your favorite font(s) from here.
  2. Practice the .text2art() command. Notice it is from the art package we imported earlier! The first input of this command is a String message, and the second input is a String value of the font you chose with the link above!!

3. Now, let’s practice allowing fun characters to speak. First, choose a favorite character from this list:

4. Then, simply use the name of the character as the name of the command, from the cowsay package. Here are some examples for you so you can see the pattern of <name of imported code package>.<name of character>()

5. Lastly, how do we allow our code to be more interactive? Sometimes, we may wish the computer to take a pause before each message…and not speak everything super fast! Is there a way for this?

Yes! The time package allows us to use a cool function called .sleep() that takes as input the # of seconds you want the computer to pause. Practice the time.sleep() command with a dictionary. Remember, we can loop through a dictionary’s keys! Try the code below. Your code output will look different since you chose different images of course, and used your own labels!

10. Practice pays off. Now the fun stuff!

You’ve done the hard part – practice. Now simply put your magic at your fingertips in action! The basic steps would be:

  1. Greet the user!
  2. Tell them they can choose from different images by printing out the image labels!
  3. Show ASCII art based on their choice :) But the cool part here is that you can use bubble letters, cool fonts - even a stegosaurus to speak to the user! Have fun.

Ready to build even bigger things? Take your Python journey to the next level with our live Python AI classes for kids. You'll master key coding skills, build interactive apps, and create amazing games under the guidance of expert instructors. Start your free trial session today!

Here Is Our Python Turtle Graphics Code Example

Code:

Output:

Enjoy This Fun Python Turtle Art Tutorial To Make Cool Graphics

Awesome job! Which art was your favorite? Ours is the cat on a fence. Hope your child enjoyed this tutorial, and if they want to learn more awesome things about Python, we would love to welcome them to our live online Python for AI class. Next, your student might enjoy trying these interesting Python exercises for kids.