Last updated: Apr 8, 2024
Reading timeยท8 min
To convert an enum to a string in Python:
Color.RED.name
.Color.RED.value
.str()
class to convert the value to a string.from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' # ๐๏ธ Human-readable string representation print(Color.RED) # ๐๏ธ Color.RED # ๐๏ธ repr() shows the value as well print(repr(Color.RED)) # ๐๏ธ <Color.RED: 'stop'> # ๐๏ธ Get the name of enum print(Color.RED.name) # ๐๏ธ RED # ๐๏ธ Get the value of enum print(Color.RED.value) # ๐๏ธ stop # ๐๏ธ If the enum value is int, you can convert it to str print(str(Color.RED.value)) # ๐๏ธ stop
name
and value
properties on an enum member to get the enum's name and value.If the value is not a string and you need to convert it to one, pass it to the str() class.
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' # ๐๏ธ if enum value is int, you can convert it to str print(str(Color.RED.value)) # ๐๏ธ stop
The str()
class returns the string representation of the supplied value.
__str__
method in the Enum classAlternatively, you can implement the __str__()
method in the class.
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' def __str__(self): return str(self.value) print(Color.RED) # ๐๏ธ "stop"
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 on the enum member.
You can also use square brackets to access enum members.
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' name = 'RED' print(Color[name].value) # ๐๏ธ 'stop' print(Color['RED'].value) # ๐๏ธ 'stop'
You can use a simple for loop if you need to iterate over an enum.
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' for color in Color: print(color) print(color.name, color.value)
You can use a list comprehension to check if a specific value is in an enum.
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' # ๐๏ธ Check enum member type print(type(Color.RED)) # ๐๏ธ <enum 'Color'> # ๐๏ธ Check if member belongs to Enum print(isinstance(Color.RED, Color)) # ๐๏ธ True values = [member.value for member in Color] print(values) # ๐๏ธ ['stop', 'go', 'get ready'] if 'stop' in values: # ๐๏ธ this runs print('stop is in values')
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
To convert a string to an enum in Python:
Enum
class from the enum
module.Enum
class.from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' # ๐๏ธ Human-readable string representation print(Sizes.MEDIUM) # ๐๏ธ Sizes.MEDIUM # ๐๏ธ repr() shows the value as well print(repr(Sizes.MEDIUM)) # ๐๏ธ <Sizes.MEDIUM: 'md'> # ๐๏ธ Get the name of the enum print(Sizes.MEDIUM.name) # ๐๏ธ MEDIUM # ๐๏ธ Get the value of the enum print(Sizes.MEDIUM.value) # ๐๏ธ md # ๐๏ธ If the enum value is int, you can convert it to str print(str(Sizes.MEDIUM.value)) # ๐๏ธ md # ๐๏ธ Access enum member using a string print(Sizes['LARGE'].value) # ๐๏ธ lg
Enums are created using the class syntax and enum members (e.g. Sizes.SMALL
)
can be anything: int
, str
, etc.
The example creates an enumeration with 3 members that all have values of type string.
name
and value
properties on an enum member to get the enum's name and value.You can also use square brackets to access enum members.
from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' name = 'MEDIUM' print(Sizes[name].value) # ๐๏ธ md print(Sizes['LARGE'].value) # ๐๏ธ lg
Here is an example that takes a string from user input
and uses it to access
an enum member.
from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' name = input('SMALL, MEDIUM or LARGE: ') print(name) # ๐๏ธ small print(Sizes[name.upper()]) # ๐๏ธ Sizes.SMALL print(Sizes[name.upper()].value) # ๐๏ธ sm
You can use a simple for
loop if you need to iterate over an enum.
from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' for size in Sizes: print(size) print(size.name, size.value)
You can use a list comprehension to check if a specific value is in an enum.
from enum import Enum class Sizes(Enum): SMALL = 'sm' MEDIUM = 'md' LARGE = 'lg' # ๐๏ธ check enum member type print(type(Sizes.MEDIUM)) # ๐๏ธ <enum 'Sizes'> # ๐๏ธ check if member belongs to Enum print(isinstance(Sizes.MEDIUM, Sizes)) # ๐๏ธ True values = [member.value for member in Sizes] print(values) # ๐๏ธ ['sm', 'md', 'lg'] if 'md' in values: # ๐๏ธ this runs print('md is in values')
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
Use the IntEnum
class from the enum
module to convert an enum to an integer
in Python.
You can use the auto()
class if the exact value is unimportant. To get a value
of an enum member, use the value
attribute on the member.
from enum import IntEnum class Sizes(IntEnum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1)) # ๐๏ธ Sizes.SMALL print(Sizes.SMALL.name) # ๐๏ธ SMALL print(Sizes.SMALL.value) # ๐๏ธ 1
The Sizes(1)
syntax allows us to pass an integer to the class and get the
corresponding enum member.
You can also use the auto()
class if the exact value of the enum members is
unimportant.
from enum import IntEnum, auto class Sizes(IntEnum): SMALL = auto() MEDIUM = auto() LARGE = auto() # ๐๏ธ [<Sizes.SMALL: 1>, <Sizes.MEDIUM: 2>, <Sizes.LARGE: 3>] print(list(Sizes)) print(Sizes.SMALL.name) # ๐๏ธ SMALL print(Sizes.SMALL.value) # ๐๏ธ 1
By default, the initial value when using the
auto class starts at
1
.
Extending from the IntEnum class is useful if you have to compare members of the enum to integers.
from enum import IntEnum class Sizes(IntEnum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes.SMALL == 1) # ๐๏ธ True print(Sizes.MEDIUM == 2) # ๐๏ธ True
Alternatively, you can use the more generic Enum
class from the enum
module.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1)) # ๐๏ธ Sizes.SMALL print(Sizes.SMALL.name) # ๐๏ธ SMALL print(Sizes.SMALL.value) # ๐๏ธ 1
You can also use square brackets to programmatically access enum members.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 name = 'MEDIUM' print(Sizes[name].value) # ๐๏ธ 2 print(Sizes['SMALL'].value) # ๐๏ธ 1
You can use a simple for
loop if you need to iterate over an enum.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 for size in Sizes: print(size) print(size.name, size.value)
You can use a list comprehension to check if a specific value is in an enum.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # ๐๏ธ check enum member type print(type(Sizes.MEDIUM)) # ๐๏ธ <enum 'Sizes'> # ๐๏ธ check if member belongs to Enum print(isinstance(Sizes.MEDIUM, Sizes)) # ๐๏ธ True values = [member.value for member in Sizes] print(values) # ๐๏ธ [1, 2, 3] if 2 in values: # ๐๏ธ this runs print('2 is in values')
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
Use parentheses to convert an integer to an enum in Python, e.g.
print(Sizes(1))
.
You can pass the integer value of an enum member as an argument to the class and it will return the enum member.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1)) # ๐๏ธ Sizes.SMALL print(Sizes(2)) # ๐๏ธ Sizes.MEDIUM print(Sizes(3)) # ๐๏ธ Sizes.LARGE print(Sizes(1).name) # ๐๏ธ SMALL print(Sizes(1).value) # ๐๏ธ 1
The Sizes(1)
syntax allows us to pass an integer to the class and get the
corresponding enum member.
You could also extend from the IntEnum class if you have to compare members of the enum to integers.
from enum import IntEnum class Sizes(IntEnum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1)) # ๐๏ธ Sizes.SMALL print(Sizes(2)) # ๐๏ธ Sizes.MEDIUM print(Sizes(3)) # ๐๏ธ Sizes.LARGE print(Sizes(1).name) # ๐๏ธ SMALL print(Sizes(1).value) # ๐๏ธ 1 print(Sizes.SMALL == 1) # ๐๏ธ True print(Sizes.MEDIUM == 2) # ๐๏ธ True
You can also use square brackets to programmatically access enum members.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 name = 'MEDIUM' print(Sizes[name].value) # ๐๏ธ 2 print(Sizes['SMALL'].value) # ๐๏ธ 1
Here is an example that takes user input
and uses it to access an enum member.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 value = input('Pick size - 1, 2 or 3: ') print(value) # ๐๏ธ 2 print(Sizes(int(value))) # ๐๏ธ Sizes.MEDIUM print(Sizes(int(value)).name) # ๐๏ธ MEDIUM print(Sizes(int(value)).value) # ๐๏ธ 2
You can use a simple for
loop if you need to iterate over an enum.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 for size in Sizes: print(size) print(size.name, size.value)
You can use a list comprehension to check if a specific value is in an enum.
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # ๐๏ธ check enum member type print(type(Sizes.MEDIUM)) # ๐๏ธ <enum 'Sizes'> # ๐๏ธ check if member belongs to Enum print(isinstance(Sizes.MEDIUM, Sizes)) # ๐๏ธ True values = [member.value for member in Sizes] print(values) # ๐๏ธ [1, 2, 3] if 2 in values: # ๐๏ธ this runs print('2 is in values')
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.
To convert an enum to JSON, extend from the str
or int
classes when
declaring your enumeration class.
You will then be able to serialize the enum members to JSON directly by using
the json.dumps()
method.
from enum import Enum import json # ๐๏ธ subclass str class first, then Enum class Color(str, Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' my_json = json.dumps(Color.RED) print(my_json) # ๐๏ธ "stop" my_str = json.loads(my_json) print(my_str) # ๐๏ธ 'stop'
The Color
class is a subclass of both str()
and Enum
.
You can use the int
class to create an enum with constants that are also
subclasses of int
.
from enum import Enum import json class Color(int, Enum): RED = 1 GREEN = 2 YELLOW = 3 my_json = json.dumps(Color.RED) print(my_json) # ๐๏ธ "1" my_int = json.loads(my_json) print(my_int) # ๐๏ธ 1
This is very similar to using the
IntEnum class from
the enum
module.
from enum import IntEnum import json class Color(IntEnum): RED = 1 GREEN = 2 YELLOW = 3 my_json = json.dumps(Color.RED) print(my_json) # ๐๏ธ "1" my_int = json.loads(my_json) print(my_int) # ๐๏ธ 1
Instead of extending from int
and Enum
, we extended from the IntEnum
class
to achieve the same result.
Note that there are some rules when working with derived enumerations:
Enum
, specify mix-in types before the Enum
class.Enum
can have members of any type, once additional types are mixed,
all the members in the enum must have values of the specified type (e.g.
int
).You can learn more about the related topics by checking out the following tutorials: