How does `print("hello world!")` output "hello world!" to `sysout`? - An Introduction to the World of CPython
The world of low-level programming is always new

The world of low-level programming is always new

Python is often called the most human-friendly language among the countless programming languages out there. Thanks to its syntax, which reads like English, even beginners can understand the code relatively easily. That’s why, as a backend developer working with Python, I’ve always focused on the question: “How can I write code that helps people understand my code and grasp the business logic more clearly?”
One day, while working on API development and building background tasks as usual, a thought struck me. 근데.. 이 파이썬은 '어떻게' 코드가 실행 되는 것이지? In other words, I became curious about the inner workings—how the code I wrote is read, how it’s interpreted, and what process it goes through to produce the output—things I had previously taken for granted.
In this post, driven by this sudden curiosity,
“Starting with a single line like print("hello world!"), I’ll explain how Python interprets and executes this code,” I’d like to explore the world of CPython.
This is basic knowledge about Python, but Python is an interpreted language.
An interpreter is a computer program or environment that executes the source code of a programming language directly. This contrasts with a compiler, which translates source code into machine code. – Wikipedia
Since execution occurs line by line as the interpreter reads the code, there is no separate compiled executable file; instead, the source code is executed directly. This is a key difference from compiled languages.
Python’s interpreter is written in C, and the widely used standard implementation of Python is also known as “Cython.” If you’re curious about Cython, you can visit the GitHub repository to view the source code.
| Interpreter (Python Implementation) | Language | Features |
|---|---|---|
| CPython | C | Most common; the Python we generally use |
| PyPy | RPython | Faster, but not fully compatible |
| Jython | Java | Runs Python in a Java environment |
| IronPython | C# | Runs Python on a .NET platform |
So how does this interpreter parse a .py file and execute the parsed code? Execution generally occurs through the following three steps.
파싱 (Parsing): The code is first broken down into tokens by a parser, and based on these tokens, a data structure called an Abstract Syntax Tree (AST) is generated
컴파일 (Compilation): The AST is converted into Python bytecode. This bytecode is saved as the .pyc file
실행 (Execution): The bytecode is executed line by line on Python’s virtual machine. In CPython, this operation is handled by the bytecode execution loop implemented in the ceval.c file
All of this happens in a matter of seconds the moment we run the file python script.py (assuming we created a file named `script.py) from the command line.
print("hello world")?! 🖨️What internal journey does print("hello world!"), the very first example in any programming language, go through?
print("hello world!")
We’ll explain the execution process of the Python code above by breaking it down into the steps described earlier: 파싱(parsing), 컴파일(Compilation), and 실행(execution).
First, the code is parsed into tokens. print("hello world!") is parsed into a function call node (Call), an identifier (Name: print), and a string constant (Constant: "hello world!"). If each parsed token is valid, an Abstract Syntax Tree (AST) is constructed.
ast module (a Python module).
pyimport ast tree = ast.parse("print('hello world!')") print(ast.dump(tree, indent=4))

Starting with Python 3.9, the parser PEG parser is used; you can examine pegen.c and pegen.h in the directory here to see this.
The generated AST is compiled into bytecode. To directly verify the execution order of this bytecode, you can use the dis built-in module as shown below.
Code
pyimport dis dis.dis("print('hello world!')")
dis module? 👉 A built-in module that disassembles Python code into bytecodeOutput 
During this process, the string constant "hello world!" is stored in the constant pool, and its value is later loaded via the LOAD_CONST instruction during the execution phase. The relevant definitions can be found in the constant pool structure in code.h.
이름 로딩 (LOAD_NAME): The interpreter checks whether a name called print exists in the current scope. Since this is a built-in function, it is looked up in the builtins module.
builtin_print_impl, and the builtin script where this function resides isprint, writing the syntax print actually executes builtin_print.상수 로딩 (LOAD_CONST): Load the string "hello world!" into memory as a constant
함수 호출 (CALL_FUNCTION): The print function is called with the above constant passed as its string argument.
실제 출력 처리: The print function internally calls Python’s sys.stdout.write() to output the string to the terminal (standard output).
We’ve examined the process by which print("hello world!")—which seems very simple on the surface but is actually quite complex internally—is printed to the terminal.

This process of digging deeper served as an opportunity to organically weave together the fragmented pieces of knowledge I had previously acquired. The insights I’ve synthesized are as follows:
While summarizing this post and examining the internal execution logic of Python—which I’d been using so familiarly—it felt as if a whole new world had opened up. Since I’m just taking my first steps in exploring CPython, I thought it would be good to delve into some lower-level study as well.