Python

Python String to Enum Conversion

An Enum or enumeration is a set of symbolic names assigned to constant and unique values. In enum, the member of Enum can be compared by their identities, and Enum itself can be repeatedly iterated. Enum variables are similar to the global variable; however, they provide more useful type safety, grouping, and other valuable features. Moreover, Enumerations are specifically used to represent the constants, so it is recommended to use the UPPER CASE variable for the members of Enum.

Module Contents of Enum Class:

There are four module content or Enumeration classes that are used to define unique sets of values and names. The four enumeration classes are IntFlag, Flag, IntEnum, and Enum. Moreover, it also provides a helper, auto, and a decorator, Unique(). Enum class is created as ‘class enum.Enum’. It is a base class that is utilized to create enumerated constants. IntEnum class is created as ‘class enum.IntEnum’. The IntEnum enumeration class is another base class used to create enumerated constants, the subclasses of int. The next class is Flag, which is created as ‘class enum.Flag’.

Flag creates the enumerated constants that can be combined by using bitwise operations without losing the Flag membership. The IntFlag enumeration class has the same functionality as the Flag enumeration class. It also creates enumerated constants that bitwise operators can combine without losing the IntFlag membership. The decorator, unique() is created as ‘enum.unique()’, which ensures that only value is associated with one name. Last but not least, the helper auto is created as ‘class enum.auto’. It is used to replace the instances of Enum members with a suitable value. However, the starting value begins at 1, by default.

Example 1:

Our first example is about creating the enum class. As it is observed that Enums are created using the class syntax, which makes it easy to write and read. Enums are extremely helpful where one variable can acquire one of a partial collection of values. Here is an instance of how to define an Enum class:

from enum import Enum

class Months(Enum):

JANUARY = 1

FEBURARY = 2

MARCH = 3

APRIL = 4

MAY = 5

JUNE = 6

Shape Description automatically generated with medium confidence

Notice that creating an Enum class itself is very simple, just like writing a class that is inherited from Enum itself. Moreover, the value of an Enum’s member may or may not be essential; however, either way, the member’s value is used to extract the corresponding member. Look at the example given below:

When you call the member of Enum ‘Months’, you will get the following output:

Example 2:

The other way to get the Enum’s member is shown in the example below:

Months.MAY

Here is the output of the above statement.

As you can see, both ways you can get the members of Enum class.

Note: the values of Enum members could be anything; str, int, etc. when the exact value is not important, the instances of helper ‘auto’ can be used so that a suitable value can be chosen. However, you need to be careful while mixing ‘auto’ helper with other values.

Example 3:

You can observe that creating and defining Enum is very simple, just like creating or defining any other class. However, here we have a simple example to help you understand converting python string to Enum. Let us run the code given below and see what we get after executing it:

from enum import Enum

class Profit(Enum):

PURCHASE = 50000

SALE = 10000

Profit['PURCHASE']

Shape Description automatically generated with medium confidence

Copy-paste the code or make your own customized Enum class by simply following the directions and seeing the output. Enter the subsequent command to convert the python string to enum.

The following output will be produced when you enter the code given above:

Example 4:

Another example of converting the python string to Enum is given below:

class stringEnum(Enum):

= 'abc'

= 456

print(stringEnum('abc'), stringEnum(456))

Text Description automatically generated

When you execute the above statements, you will get the following output:

As you can see, the string is converted to both str() and int(). The value of Enum member can be extracted in either way.

Conclusion:

This article uses the correct way to convert the python string to enum. First, we have discussed what Enum is and how it can be used. There are four classes of Enumeration; Enum, IntEnum, Flag, IntFlag, and there is one helper, ‘auto’, and one decorator, unique(). Enumeration classes are bases classes used to create enumerated constants. The helper ‘auto’ is used to replace the instances of Enum members with a suitable value. While the decorator, unique() is used to ensure that only one value is allocated to a name. After discussing the enumeration classes, we have learned how to create an Enum class and how it can be used for python string to enum conversion. This article will help you understand the python string conversion to Enum and will guide you to make your own Enum classes without making any mistakes. Follow the directions and start making your enum classes for python string to enum conversion.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content