Why does print ‘format’ the text, even though it doesn’t actually format anything?
In this video, we’ll explore what actually happens when Python encounters escape sequences and other control characters.
Why does `print()` wrap lines? Who changes the text colour in the terminal? What exactly does Python interpret, and what is handled by the terminal itself? And why might understanding this process come in handy during a technical interview?
In this video, we’ll cover:
- how Python processes escape sequences within string literals;
- how Python escape sequences differ from terminal control sequences;
- how ANSI/CSI sequences work;
- why the terminal maintains its state;
- how to change the colour and style of text, the cursor position, and clear the screen;
- what happens when stdout is redirected to a file;
- and what sort of question on this topic might well be asked in a Python interview.
Timecodes
00:00 Introduction
00:43 Why `print()` isn’t as simple as it seems
02:15 What are escape sequences?
05:48 Python escape sequences and ANSI terminal commands
06:18 How ANSI sequences change text colour
10:37 Why the terminal maintains its state
12:57 Combining ANSI commands
13:54 Clearing the screen and terminal control
15:19 CSI, OSC and other control sequences
16:28 Questions about `print()` in Python job interviews
20:00 What `print()` actually sends to stdout
23:21 Who is responsible for what: Python, `print()` or the terminal
24:17 Conclusion