Skip to main content

Overview

Python strings are Unicode (UTF-8) text. The C API provides extensive functions for creating and manipulating Unicode strings.

Type Object

Type Checking

Creating Unicode Strings

From C Strings

Create from UTF-8 encoded C string (null-terminated). Example:

With Length

Example:

From Format String

Like sprintf for Python strings. Format codes:
  • %s - C string (UTF-8)
  • %d, %ld - Integers
  • %S - PyObject* (via PyObject_Str())
  • %R - PyObject* (via PyObject_Repr())
Example:

Converting to C Strings

Get UTF-8 encoded C string. Returns NULL on error. Example:
The returned pointer is owned by the Unicode object. Don’t free it!

String Operations

Example:

Complete Example

See Also