Content · Programming · Python

Python enum.IntEnum and enum.StrEnum: Usage and Examples

Choose IntEnum or StrEnum when an Enum should also behave like a scalar.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

Choose IntEnum or StrEnum when an Enum should also behave like a scalar.

What You Will Learn


IntEnum subclasses int and allows numeric comparisons; StrEnum subclasses str and behaves like a string in many contexts. Both retain Enum semantics and named members, but cross-type equality needs care.

When API code requires exact str type, convert with str(member) if necessary.

Class interfaces should preserve their stated invariants. Type annotations describe intent but do not enforce runtime types on their own.

Step-by-Step Example


The example below makes the behavior visible with small input values that you can change safely.

from enum import IntEnum, StrEnum

class HttpCode(IntEnum):
    OK = 200

class Theme(StrEnum):
    DARK = "dark"

print(HttpCode.OK == 200)
print(Theme.DARK == "dark")

How the Example Works


  • The imports provide the standard-library tools used in the demonstration: enum.
  • The definitions HttpCode, Theme separate the main steps into named components; calls to those components drive the example.
  • The print() calls make the return values or state visible so you can compare them with the explanation and check your prediction.

Expected output from the example:

True
True

When You Would Use This


A class becomes useful when several operations must maintain rules around related data. Think about which state belongs to each instance, which operations should be public, and how errors should be reported. Avoid making the design more complex than the underlying task.

Practice: Modify the Code


Create a StrEnum for application modes and inspect each value.

Verify the changed output against the behavior described above, including the relevant invalid or boundary case.

For complete parameters and version details, consult the official Python documentation.

Common Beginner Mistakes


  • Using IntEnum for purely descriptive states that should not compare equal to raw integers.
  • Assuming plain Enum members compare like their underlying scalar values.

Key Takeaways


  • Choose IntEnum or StrEnum when an Enum should also behave like a scalar.
  • IntEnum subclasses int and allows numeric comparisons; StrEnum subclasses str and behaves like a string in many contexts.
  • When API code requires exact str type, convert with str(member) if necessary.
Share this resourceSend this page to someone who may find it useful.