Well, they have a name: ANSI escape codes (or CSI for short). And boy oh boy do they add some spice to our lives.
First, how these bad boys work. When you want to print something on the screen, your computer sends a signal to the terminal or console that says “hey, I want this text to appear here”. But what if we wanted to make it bold? Well, instead of just sending the text itself, we send an escape code followed by some instructions for how to format it.
For example, let’s say you want to print the word “hello” in bold. You would use this command: `\033[1mhello\033[0m` (note that I used backslashes instead of forward slashes because they are not recognized by most text editors). The \033 is an escape code, and the [1m tells your terminal to turn on bold mode. Then you print “hello”, followed by another escape code (\033[0m) that turns off bold mode.
Now some of our favorite control characters! Here are a few:
– \033[1m Bold (turns on bold mode)
– \033[2m Dim (diminishes the text)
– \033[3m Underline (underlines the text)
– \033[4m Blink (blinks the text)
– \033[5m Reverse (reverses the foreground and background colors)
– \033[7m Invisibility (hides the text from view)
– \033[8m Crossed out (crosses out the text with a line through it)
These are just a few examples, but there are many more! You can find a full list of ANSI escape codes here: https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(0)_parameters
Now some practical applications for these control characters. For example, you could use them to create a progress bar in your terminal! Here’s an example script that uses ANSI escape codes to print out a progress bar:
# Import the time module to use its functions
import time
# Define a function to create a progress bar
def progress_bar(progress):
# Calculate the number of bars we want to display based on our progress
num_bars = int((progress / 100) * 50)
# Print out a line with spaces and control characters for each bar
# Use the carriage return character to move the cursor back to the beginning of the line
print("\r", end="")
# Use a for loop to print out the desired number of bars
for i in range(num_bars):
# Use the escape character to print a vertical bar
print("█", end="")
# Use another for loop to print out the remaining spaces
for j in range(50 - num_bars):
print(" ", end="")
# Print out the progress percentage and update it every second
# Calculate the percentage and format it to two decimal places
percent = int((progress / 100) * 100)
# Use the escape character to move the cursor back to the beginning of the line
print(f"{percent:.2f}%", end="\r")
# Use the time module's sleep function to pause the program for 1 second
time.sleep(1)
This script uses a for loop to iterate through the number of bars we want to display based on our progress, and then prints out control characters (in this case, “” for solid blocks and ” ” for spaces). The “\r” at the beginning of each line clears the current line in the terminal.
And that’s it! With a little bit of creativity and some ANSI escape codes, you can add some spice to your text output and make your code more visually appealing.