It seems that it is enough to inherit from str class at the same time as Enum:

from enum import Enum

class MyEnum(str, Enum):
    state1 = 'state1'
    state2 = 'state2'

The tricky part is that the order of classes in the inheritance chain is important as this:

class MyEnum(Enum, str):
    state1 = 'state1'
    state2 = 'state2'

throws:

TypeError: new enumerations should be created as `EnumName([mixin_type, ...] [data_type,] enum_type)`

With the correct class the following operations on MyEnum are fine:

print('This is the state value: ' + state)

As a side note, it seems that the special inheritance trick is not needed for formatted strings which work even for Enum inheritance only:

msg = f'This is the state value: {state}'  # works without inheriting from str
Answer from sophros on Stack Overflow
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ library โ€บ enum.html
enum โ€” Support for enumerations
February 23, 2026 - StrEnum is the same as Enum, but ... on or with a StrEnum member is not part of the enumeration. >>> from enum import StrEnum, auto >>> class Color(StrEnum): ......
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ enum.html
Enum HOWTO โ€” Python 3.14.4 documentation
For example, if the class was made available in class SomeData in the global scope: >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal') ... Enum( value='NewEnumName', names=<...>, *, module='...', qualname='...', ...
Discussions

Convert string to Enum in Python - Stack Overflow
This is exactly the opposite of an regular Enum that was assigned strings as the values. 2023-10-17T14:24:30.01Z+00:00 ... Example code prints 200 for both cases with Python 3.11 and 3.13, BuildType("debug") will raise a ValueError. 2025-10-31T10:52:20.92Z+00:00 ... >>> from enum import Enum ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Getting value of enum on string conversion - Stack Overflow
If you are going to print value using f-string then you can inherit your enum from both Enum and str. This way you can have both value and name of the object. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Find enum value by enum name in string - Python - Stack Overflow
I'm struggling with Python enums. I created an enum class containing various fields: class Animal(Enum): DOG = "doggy" CAT = "cute cat" I know that I can access this enum... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there a way to somehow link string with enum?
Youโ€™re doing a runtime mapping of a (possibly weirdly cased) string to an int. One solution is a simple function with an array of const strings (Sunday, Monday, Tuesday etc.). Iterate over them in for loop and strncmpi - get a match? Break. The index into the array is your int. You may wish to rebase from 0 to 1. You definitely want a default case for invalid inputs (say -1). More on reddit.com
๐ŸŒ r/cprogramming
16
13
September 23, 2024
Top answer
1 of 8
217

It seems that it is enough to inherit from str class at the same time as Enum:

from enum import Enum

class MyEnum(str, Enum):
    state1 = 'state1'
    state2 = 'state2'

The tricky part is that the order of classes in the inheritance chain is important as this:

class MyEnum(Enum, str):
    state1 = 'state1'
    state2 = 'state2'

throws:

TypeError: new enumerations should be created as `EnumName([mixin_type, ...] [data_type,] enum_type)`

With the correct class the following operations on MyEnum are fine:

print('This is the state value: ' + state)

As a side note, it seems that the special inheritance trick is not needed for formatted strings which work even for Enum inheritance only:

msg = f'This is the state value: {state}'  # works without inheriting from str
2 of 8
181

By reading the documentation (i.e., I didn't try it because I use an older version of Python, but I trust the docs), since Python 3.11 you can do the following:

from enum import StrEnum

class Direction(StrEnum):
    NORTH = 'north'
    SOUTH = 'south'

print(Direction.NORTH)
>>> north

Note that it looks like when subclassing StrEnum, defining the enum fields as single-value tuples will make no difference at all and would also be treated as strings, like so:

class Direction(StrEnum):
    NORTH = 'north',    # notice the trailing comma
    SOUTH = 'south'

Please refer to the docs and the design discussion for further understanding.

If you're running python 3.6+, execute pip install StrEnum, and then you can do the following (confirmed by me):

from strenum import StrEnum

class URL(StrEnum):
    GOOGLE = 'www.google.com'
    STACKOVERFLOW = 'www.stackoverflow.com'

print(URL.STACKOVERFLOW)

>>> www.stackoverflow.com

You can read more about it here.


Also, this was mentioned in the docs - how to create your own enums based on other classes:

While IntEnum is part of the enum module, it would be very simple to implement independently:

class IntEnum(int, Enum): pass This demonstrates how similar derived enumerations can be defined; for example a StrEnum that mixes in str instead of int.

Some rules:

When subclassing Enum, mix-in types must appear before Enum itself in the sequence of bases, as in the IntEnum example above.

While Enum can have members of any type, once you mix in an additional type, all the members must have values of that type, e.g. int above. This restriction does not apply to mix-ins which only add methods and donโ€™t specify another type.

When another data type is mixed in, the value attribute is not the same as the enum member itself, although it is equivalent and will compare equal.

%-style formatting: %s and %r call the Enum classโ€™s str() and repr() respectively; other codes (such as %i or %h for IntEnum) treat the enum member as its mixed-in type.

Formatted string literals, str.format(), and format() will use the mixed-in typeโ€™s format() unless str() or format() is overridden in the subclass, in which case the overridden methods or Enum methods will be used. Use the !s and !r format codes to force usage of the Enum classโ€™s str() and repr() methods.

Source: https://docs.python.org/3/library/enum.html#others

๐ŸŒ
Cosmicpython
cosmicpython.com โ€บ blog โ€บ 2020-10-27-i-hate-enums.html
Making Enums (as always, arguably) more Pythonic
Well, the docs say you can just subclass str and make your own StringEnum that will work just like IntEnum. But itโ€™s LIES: class BRAIN(str, Enum): SMALL = 'small' MEDIUM = 'medium' GALAXY = 'galaxy' assert BRAIN.SMALL.value == 'small' # ok, as before assert BRAIN.SMALL == 'small' # yep assert list(BRAIN) == ['small', 'medium', 'galaxy'] # hooray!
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-convert-enum-to-string
Convert an Enum to a String and vice versa in Python | bobbyhadz
April 8, 2024 - The example creates an enumeration with 3 members that all have values of type string. You can use the name and value properties on an enum member to get the enum's name and value.
๐ŸŒ
Real Python
realpython.com โ€บ python-enum
Build Enumerations of Constants With Python's Enum โ€“ Real Python
December 15, 2024 - A classic example of when you should use an enumeration is when you need to create a set of enumerated constants representing the days of the week. Each day will have a symbolic name and a numeric value between 1 and 7, inclusive.
๐ŸŒ
Not Invented Here
notinventedhere.org โ€บ articles โ€บ python โ€บ how-to-use-strings-as-name-aliases-in-python-enums.html
How to use strings as name aliases in Python enums - Not Invented Here
April 9, 2017 - Python has support for enumerations built into the standard library since version 3.4. The Enum type is quite powerful but serializing enum members to human readable representations and deserializing them to an enum meber can be cumbersome. If we want to have an enumeration of certain train stations between the two Austrian cities Wien and Wels, the following approach comes to mind: from enum import Enum class Station(Enum): wien_westbahnhof = 1 st_poelten = 2 linz = 3 wels = 4
Find elsewhere
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ enum
Python enum: simplify code with easy-to-read enumerations
It ensures compatibility with string operations while retaining all the benefits of enums. ... from enum import StrEnum class Color(StrEnum): RED = "red" GREEN = "green" BLUE = "blue" print(Color.RED) # Outputs: 'Color.RED' print(Color.RED.value) # Outputs: 'red'
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ enum-in-python
enum in Python - GeeksforGeeks
June 20, 2024 - In this example, we will use for loop to print all the members of the Enum class. The code defines an enumeration class 'Season' with four members. It iterates through the enum members and prints their values and names.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ enum-in-python
Enum in Python
August 24, 2023 - from enum import Enum, unique @unique class subjects(Enum): ENGLISH = 1 MATHS = 2 GEOGRAPHY = 3 SANSKRIT = 2 ... @unique ^^^^^^ raise ValueError('duplicate values found in %r: %s' % ValueError: duplicate values found in <enum 'subjects'>: SANSKRIT -> MATHS ยท The Enum class is a callable class, hence you can use its constructor to create an enumeration. This constructor accepts two arguments, which are the name of enumeration and a string consisting of enumeration member symbolic names separated by a whitespace.
๐ŸŒ
Python
docs.python.org โ€บ 3.11 โ€บ โ€บ howto โ€บ enum.html
Enum HOWTO โ€” Python 3.11.14 documentation
March 14, 2019 - For example, if the class was made available in class SomeData in the global scope: >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal') ... Enum( value='NewEnumName', names=<...>, *, module='...', qualname='...', ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-lookup-enum-by-string-value
Python Program to Lookup enum by String value
April 17, 2023 - To look up an enum by string value we need to follow the following steps: ... Create a function that takes an enum string as input and returns the corresponding enum value. from enum import Enum class ClassName(Enum): Key_1= Value_1 Key_2= Value_2 Key_3= Value_3
๐ŸŒ
Python
docs.python.org โ€บ 3.10 โ€บ library โ€บ enum.html
enum โ€” Support for enumerations โ€” Python 3.10.20 documentation
For example, if the class was made available in class SomeData in the global scope: >>> Animal = Enum('Animal', 'ANT BEE CAT DOG', qualname='SomeData.Animal') ... Enum(value='NewEnumName', names=<...>, *, module='...', qualname='...', type=<mixed-in class>, start=1) ... What the new Enum class ...
๐ŸŒ
Pecar
blog.pecar.me โ€บ python-enum
Enum with `str` or `int` Mixin Breaking Change in Python 3.11 | Anลพe's Blog
from enum import Enum class Foo(str, Enum): BAR = "bar" Foo.BAR in Python 3.11 will no longer return the member value "bar" when used in the format() function or f-strings the way that prior Python versions used to.
๐ŸŒ
ZetCode
zetcode.com โ€บ python โ€บ enum
Python enum - working with enumerations in Python
The next example presents some other basic functionality of a Python enum. ... #!/usr/bin/python from enum import Enum class Season(Enum): SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4 seas = Season.SPRING print(seas) print(isinstance(seas, Season)) print(type(seas)) print(repr(seas)) print(Season['SPRING']) print(Season(1)) Again, we deal with a Season enumeration created with the class. ... Here we print a human readable string representation of a Season member.
๐ŸŒ
Tsak
tsak.dev โ€บ posts โ€บ python-enum
The case for StrEnum in Python 3.11 - tsak.dev
In Python 3.11, the difference in using an Enum entry in a string context was changed, so now it returns the stringified reference instead. In our codebase, we had to change the use of enum entries to explicitly call Foo.BAR.value wherever we had used an enum entry in a format context. # Python 3.11 from enum import Enum class Foo(str, Enum): BAR = "bar" x = Foo.BAR x # Outputs <Foo.BAR: 'bar'> f"{x}" # Outputs 'Foo.BAR' "{}".format(x) # Outputs 'Foo.BAR' str(x) # Outputs 'Foo.BAR' x.value # Outputs 'bar'
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ python string to enum | example code
Python string to enum | Example code - Tutorial - By EyeHunts
April 3, 2023 - import enum class QuestionType(enum.Enum): MULTI_SELECT = "multi" SINGLE_SELECT = "single" @staticmethod def from_str(label): if label in ('single', 'singleSelect'): return QuestionType.SINGLE_SELECT elif label in ('multi', 'multiSelect'): return QuestionType.MULTI_SELECT else: raise NotImplementedError print(QuestionType.from_str('single')) ... Hereโ€™s the syntax for defining an enumeration class and converting a string to an enumeration value in Python: