Adding a third property to my enums

I have a bunch of enums as such:

class MyEnum(StrEnum):
    CUTE_CAT = "CUTE_CAT"
    HAPPY_DOG = "HAPPY_DOG"
    PANDA = "PANDA"

I know enums have a .value and a .name. Is it possible to add a third, say, .title, that returns titlecase:

>>> MyEnum.HAPPY_DOG.title
'Happy Dog'
>>> MyEnum.PANDA.title
'Panda'

I’ve been googling this a bit and I did come across this 4 years old SO that sort of achieves what I want. Is this the best way to go about it? Are there better ways to handle this?

You can add a property that manipulates enum’s .value into the form you want:

from enum import StrEnum


class MyEnum(StrEnum):
    CUTE_CAT = "CUTE_CAT"
    HAPPY_DOG = "HAPPY_DOG"
    PANDA = "PANDA"

    @property
    def title(self) -> str:
        return self.value.replace("_", " ").title()


>>> MyEnum.HAPPY_DOG.title
'Happy Dog'
2 Likes

That’s perfect! Thank you!