Python (Fall 2026) Module 7-6: Fun With Colors
Watch on YouTube →
Overview
Bill Moseley shows how Python command-line output can use ANSI escape sequences to display colored text and backgrounds, with color settings remaining active until changed or reset. He demonstrates wrapping these codes in parameterized helper functions such as `printRed` and `printYellow`, making color output reusable and useful for communicating warnings or unavailable options to users.
Key takeaways
- ANSI escape sequences let Python programs color terminal text without building a graphical user interface.
- A color escape sequence remains in effect for subsequent output until another sequence changes the color or restores the default.
- The foreground codes `93` for yellow and `39` for the default color can be wrapped in a `printYellow` helper function.
- Parameterized functions such as `printRed(strText)` make color formatting reusable for different messages across a program.
- Using colors consistently can communicate interface meaning, such as highlighting warnings in yellow or drawing attention to invalid input in red.
Chapters
- Bill Moseley introduces ANSI escape sequences as an optional, for-fun technique rather than an assignment requirement.
- The Module 7 Canvas page, “ANSI escape sequences for color in Python,” documents codes for foreground, background, and bright colors.
- Escape sequences begin with `\033[`, include a color code, and end with `m`; the selected color persists until another code changes or resets it.
- A parameter such as `strText` lets a `printRed` function apply red formatting to any supplied text, including “hello world.”
- Creating a separate helper such as `printYellow` avoids repeatedly writing the escape sequence; Moseley identifies `93` as the yellow foreground code.
- The `39` code restores the default foreground color, while `49` restores the default background color.
- Colors can signal meaning in command-line interfaces: yellow can flag a warning, while red can draw attention to an invalid input or unavailable option.
- Parameterized helper functions let programmers reuse color formatting throughout a Python program without memorizing or rewriting every ANSI code.
- The Canvas links-and-tools page provides examples and additional ANSI escape sequence options for experimenting beyond the basic notation.
Transcript excerpt
0:03 All right, welcome back everybody. We're going to get into custom functions part two. Today we're going to dig a little deeper into some extra tricks that you can do with custom functions, and then I'm going to go over the assignment for this module, and then you'll have some work time as well. But first, I'm going to introduce something that is mostly just for fun. It's not going to be required of you in any of the assignments in this class, but I am a big fan of trying to make learning new things fun. So, I thought I would share this little trick with you. And you know, so far all of our stuff that we've done has looked like this. Just boring, same color.
1:01 And that's kind of the limit. But if you go to module 7 in Canvas under links and tools here, you'll find a link to this page here. It's called ANSI escape sequences for color in Python. So, I, we're not at the point where we're making a graphical user interface yet with buttons and things like that, but if you read through this, you will see that there are special codes that you can slip into your text when you print things out that will allow you to print things in different colors. So, for example, if I want to change this text to something else, I can type a code in here
1:54 that will change that text to red. I can also change the background color of the text to different colors. So using these little codes here, and this just looks like some gibberish, right? So if I separate this out so you can see one from the other. Basically, this first string here is the code that says change the background of
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.