How to convert enum to string in python

Python

In this post, we will understand the code to How to convert enum to string in Python with examples.

1. How to convert enum to string in python


To print an enum member as a string we use the class and member name. In this example, we have enum class name FRUITS and have three members (APPLE = 1,ORANGE = 2,GRAPES = 3). We are printing enum members as FRUITS.APPLE by print() statement.

Program Example

from enum import Enum

class FRUITS(Enum):
     APPLE = 1
     ORANGE = 2
     GRAPES = 3



print ("The print enum memebr as string :\n ",end="")
print (FRUITS.APPLE)
print (FRUITS.ORANGE)
print (FRUITS.GRAPES)

print ("\n Print Name of enum member : \n",end ="")
print (FRUITS.APPLE)
print (FRUITS.ORANGE)
print (FRUITS.GRAPES)



Output

The print enum memebr as string :
 FRUITS.APPLE
FRUITS.ORANGE
FRUITS.GRAPES

 Print Name of enum member : 
FRUITS.APPLE
FRUITS.ORANGE
FRUITS.GRAPES

2. ENUM member properties


The enum class FRUITS and printing all the enum properties.As you can see enum members have two information, name and a value. We can access the enum members by using the dot operator(.) with the enum class name.

  • repr() : The repr() method used to print enum member.
  • type(): This is used to print the type of enum member.
  • FRUITS.APPLE.name: It is used to print the name of the enum memeber.

Program Example

from enum import Enum

class FRUITS(Enum):
     APPLE = 1
     ORANGE = 2
     GRAPES = 3

print('\n The print enum memeber repr:\n')
print (repr(FRUITS.APPLE))
print (repr(FRUITS.ORANGE))
print (repr(FRUITS.GRAPES))


print ("\n Print type enum member is : \n",end ="")
print (type(FRUITS.APPLE))
print (type(FRUITS.ORANGE))
print (type(FRUITS.GRAPES))

print ("\n Print Name of enum member : \n",end ="")
print (FRUITS.APPLE.name)
print (FRUITS.ORANGE.name)
print (FRUITS.GRAPES.name)

Output

 The print enum memeber repr:

<FRUITS.APPLE: 1>
<FRUITS.ORANGE: 2>
<FRUITS.GRAPES: 3>

 Print type enum member is : 
<enum 'FRUITS'>
<enum 'FRUITS'>
<enum 'FRUITS'>

 Print Name of enum member : 
APPLE
ORANGE
GRAPES

3. How to access all values of Python Enum?


We are looping the overall values of an enum using list comprehension and printing all values as a list.

Program Example

class FRUITS(Enum):
     APPLE = 1
     ORANGE = 2
     GRAPES = 3

e_valu_list = [myenum.value for myenum in FRUITS]
print('enum values:',e_valu_list)

Output

enum values: [1, 2, 3]

4. How to check member exist in Enum


Also if we want to check any member is present in an enum or not, we can do that check by using the isinstance() method like:

isinstance(): method will return true if the member is present in enum and false if it is not.

Program Example

from enum import Enum
class Color(Enum):
     RED= 7
     BLUE= 4
     GREEN= 9
     WHITE= 3
     BLACK = 2
print(isinstance(Color.GREEN, Color))

Output

True

Summary

In this post, we have learned How to convert enum to string in Python with examples also learned.

  • Convert enum to string in Python
  • ENUM member properties
  • How to access all values of Python Enum?
  • How to check member exist in Enum