Skip to main content

How to Compare Strings and Enums in Python

This guide explains how to compare strings with enum members in Python, a common task when working with user input, configuration files, or external data sources. We'll cover the best practices, including extending the Enum class, accessing enum values directly, and a less recommended approach, using OrderedEnum from the documentation.

The most direct and Pythonic way to compare strings and enums is to make your enum class inherit from both str and Enum:

from enum import Enum

class Color(str, Enum): # Inherit from str and Enum
RED = 'stop'
GREEN = 'go'
YELLOW = 'get ready'

my_str = 'go'

if my_str == Color.GREEN: # Direct comparison!
print('success') # Output: success

print(my_str == Color.YELLOW) # Output: False
  • class Color(str, Enum):: This is the key. By inheriting from str, you make each enum member a string instance in addition to being an enum member. This allows direct comparison with strings using the equality operator (==).
  • This approach is the most concise and readable way to compare strings with enums. It's also efficient, as it doesn't require any extra lookups or conversions.

Comparing Strings to Enums Using the .value Attribute

If you can't, or don't want to, inherit from str in your enum class definition, you can compare the string to the .value attribute of the enum member:

from enum import Enum

class Color(Enum): # Standard Enum, not inheriting from str
RED = 'stop'
GREEN = 'go'
YELLOW = 'get ready'

my_str = 'go'

if my_str == Color.GREEN.value: # Access the .value
print('success') # Output: success
print(my_str == Color.YELLOW.value) # Output: False
  • The Color.GREEN.value returns the value that has been associated with the enum.

You can also access enum members dynamically using square bracket notation:

name_1 = 'RED'
print(Color[name_1].value == 'stop') # Output: True

Comparing Enum Members (Values)

If your enum members have values that support comparison operations (like numbers), you can compare them directly using comparison operators (>, <, ==, etc.):

Comparing using .value

from enum import Enum
class Sizes(Enum):
SMALL = 1
MEDIUM = 2
LARGE = 3

print(Sizes.LARGE.value > Sizes.MEDIUM.value) # Output: True
print(Sizes.MEDIUM.value < Sizes.LARGE.value) # Output: True
print(Sizes.SMALL.value == Sizes.LARGE.value) # Output: False
  • This is the standard and most explicit way to compare enum values.

The Python documentation provides an example of an OrderedEnum class that implements comparison methods directly on the enum members. This is less common and can be less clear.

from enum import Enum

class OrderedEnum(Enum): # This example is copied from Python documentation
def __ge__(self, other):
if self.__class__ is other.__class__:
return self.value >= other.value
return NotImplemented
def __gt__(self, other):
if self.__class__ is other.__class__:
return self.value > other.value
return NotImplemented
def __le__(self, other):
if self.__class__ is other.__class__:
return self.value <= other.value
return NotImplemented
def __lt__(self, other):
if self.__class__ is other.__class__:
return self.value < other.value
return NotImplemented

class Sizes(OrderedEnum):
SMALL = 1
MEDIUM = 2
LARGE = 3

print(Sizes.LARGE > Sizes.MEDIUM) # Output: True
print(Sizes.MEDIUM < Sizes.LARGE) # Output: True
print(Sizes.SMALL == Sizes.LARGE) # Output: False
  • The OrderedEnum class has to be added to your code.
  • The methods are implemented on the class, which makes it possible to directly compare enum objects.