Search notes:

Python: printf using ctypes

The ctypes standard library allows use the standard C lib's implementation of printf.
Note that the use of b'…' literals is required when calling printf.

Linux

import ctypes

libc = ctypes.cdll.LoadLibrary("libc.so.6")

chars_printed = libc.printf(b'num: %d, txt: %s\n', 42, b'Hello World')

Windows

import ctypes

libc = ctypes.cdll.msvcrt

chars_printed = libc.printf(b'num: %d, txt: %s\n', 42, b'Hello World')

See also

string.format()

Links

This Stackoverflow answer was very helpful and served as basis for the information on this page.

Index