This functionality is already built in to Enum:

>>> from enum import Enum
>>> class Build(Enum):
...   debug = 200
...   build = 400
... 
>>> Build['debug']
<Build.debug: 200>

The member names are case sensitive, so if user-input is being converted you need to make sure case matches:

an_enum = input('Which type of build?')
build_type = Build[an_enum.lower()]
Answer from Ethan Furman on Stack Overflow
🌐
Python
docs.python.org › 3 › library › enum.html
enum — Support for enumerations
February 23, 2026 - These three enum types are designed to be drop-in replacements for existing integer- and string-based values; as such, they have extra limitations: __str__ uses the value and not the name of the enum member · __format__, because it uses __str__, ...
Discussions

Why do widely used frameworks in python use strings instead of enums for parameters?
I’d guess age and momentum? I don’t think enums were used nearly as much before type hinting was more common. More on reddit.com
🌐 r/Python
108
225
October 16, 2024
String-based enum in Python - Stack Overflow
I used to do this but had strange ... an enum object would get deserialised to the string when assigning to a variable. Removing the str subclass fixed it and it seems to work fine without it. 2023-09-29T09:59:42.03Z+00:00 ... 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 ... 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
🌐
Python documentation
docs.python.org › 3 › howto › enum.html
Enum HOWTO — Python 3.14.3 documentation
This can be a whitespace- or comma-separated string (values will start at 1 unless otherwise specified): 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE' ... Changed in version 3.5: The start parameter was added. The first variation of Enum that is provided is also a subclass of int. Members of an IntEnum can be compared to integers; by extension, integer enumerations of different types can also be compared to each other: >>> from enum import IntEnum >>> class Shape(IntEnum): ...
🌐
Mimo
mimo.org › glossary › python › enum
Python enum: simplify code with easy-to-read enumerations
Python's StrEnum is a specialized version of enums where all members are strings. 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: ...
🌐
Real Python
realpython.com › python-enum
Build Enumerations of Constants With Python's Enum – Real Python
December 15, 2024 - Enum, IntEnum, IntFlag, and Flag differ in their support for integer operations and bitwise flags. Enums can work with data types like integers or strings, boosting their flexibility.
🌐
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!
🌐
GeeksforGeeks
geeksforgeeks.org › python › enum-in-python
enum in Python - GeeksforGeeks
December 11, 2025 - You can use a for loop to go through all Enum members and access each member’s name and value, making it easy to display or process them. ... from enum import Enum class Week(Enum): SUNDAY = 1 MONDAY = 2 TUESDAY = 3 WEDNESDAY = 4 for s in Week: print(s.value, "-", s)
Find elsewhere
🌐
Medium
sandeepnarayankv.medium.com › python-3-11s-game-changing-strenum-and-intenum-say-goodbye-to-value-forever-778bcf5b8034
Python 3.11’s Game-Changing StrEnum and IntEnum: Say Goodbye to .value() Forever! 🚀 | by Sandeep Narayan Kizhakke Veettil | Medium
May 4, 2025 - 🧠 In Python 3.11, the Enum class got two powerful new siblings: StrEnum and IntEnum. These specialized enums simplify your code when working with string-based or integer-based constants. 🧩 Why Use StrEnum and IntEnum? Remember our old friend the basic Enum? While powerful, we often need our enums to behave exactly like strings or integers: # Old approach - basic Enum with string values from enum import Enum class Color(Enum): RED = "red" GREEN = "green" BLUE = "blue" # Problem: Comparisons require .value json_color = Color.RED json.dumps({"color": json_color}) # TypeError!
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

🌐
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 __str__() method is called by str(object) and the built-in functions format() and print() and returns the informal string representation of the object. Now you can get the value of the enum directly, without accessing the value attribute ...
🌐
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
🌐
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
🌐
Python
docs.python.org › 3.11 › › howto › enum.html
Enum HOWTO — Python 3.11.14 documentation
March 14, 2019 - This can be a whitespace- or comma-separated string (values will start at 1 unless otherwise specified): 'RED GREEN BLUE' | 'RED,GREEN,BLUE' | 'RED, GREEN, BLUE' ... Changed in version 3.5: The start parameter was added. The first variation of Enum that is provided is also a subclass of int. Members of an IntEnum can be compared to integers; by extension, integer enumerations of different types can also be compared to each other: >>> from enum import IntEnum >>> class Shape(IntEnum): ...
Top answer
1 of 7
23

[Time passes...]

The new Python Enum has finally landed in 3.4, and has also been backported. So the answer to your question is now to use that. :)


An example:

>>> from enum import Enum
>>> class Modes(Enum) :
...    Mode1 = "M1"
...    Mode2 = "M2"
...    Mode3 = "M3"
...

>>> Modes.Mode1
<Modes.Mode1: 'M1'>

>>> Modes.Mode1.value
'M1'

>>> Modes.Mode1.value
'M1'

>>> Modes['Mode1']    # index/key notation for name lookup
<Modes.Mode1: 'M1'>

>>> Modes('M1')       # call notation for value lookup
<Modes.Mode1: 'M1'>

>>> Modes("XXX")      # example error
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\lib\enum.py", line 291, in __call__
    return cls.__new__(cls, value)
  File "C:\Anaconda3\lib\enum.py", line 533, in __new__
    return cls._missing_(value)
  File "C:\Anaconda3\lib\enum.py", line 546, in _missing_
    raise ValueError("%r is not a valid %s" % (value, cls.__name__))
ValueError: 'XXX' is not a valid Modes
2 of 7
11

Well, here is what you asked for:

class MyEnum:
  VAL1, VAL2, VAL3 = range(3)
  @classmethod
  def tostring(cls, val):
    for k,v in vars(cls).iteritems():
        if v==val:
            return k

  @classmethod
  def fromstring(cls, str):
      return getattr(cls, str.upper(), None)

print MyEnum.fromstring('Val1')
print MyEnum.tostring(2)

But I really don't get the point of Enums in Python. It has such a rich type system as well as generators and coroutines to manage states.

I know I've not been using Enums in Python for more than 12 years, maybe you can get rid of them too ;-)

🌐
ZetCode
zetcode.com › python › enum
Python enum - working with enumerations in Python
The example fails with the ValueError: duplicate values found in <enum 'Season'>: WINTER -> AUTUMN error, because the AUTUMN and WINTER members have the same value. If we comment out the @unique decorator, the example prints three members; the WINTER is ignored. The special attribute __members__ is a read-only ordered mapping of names to members. ... #!/usr/bin/python from enum import Enum Season = Enum('Season', [('SPRING', 1), ('SUMMER', 2), ('AUTUMN', 3), ('WINTER', 4)]) for name, member in Season.__members__.items(): print(name, member)
🌐
Pecar
blog.pecar.me › python-enum
Enum with `str` or `int` Mixin Breaking Change in Python 3.11 | Anže's Blog
December 21, 2022 - 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.
🌐
Hrekov
hrekov.com › blog › python-enum-string-representation
Python Enum to String without Class Name | Backend APIs, Web Apps, Bots & Automation | Hrekov
December 9, 2025 - This article demonstrates how to leverage Python's dunder methods (__str__, __repr__, __format__) to gain complete control over the string representation of your Enum members. By default, an Enum member prints its full, verbose representation, including the class name and the member name, which is usually not what's desired when serializing to a simple string. from enum import Enum class Color(Enum): RED = 'red' GREEN = 'green' my_color = Color.RED # Default print output: # print(my_color) # Output: Color.RED # print(str(my_color)) # Output: Color.RED