On this page
enum.StrEnum
Prev: enum.ReprEnum | Up: enum | Next: enum.unique
Rel: enum.auto, enum.Enum, enum.EnumCheck, enum.EnumDict
Ref: docs.python.org — enum.StrEnum
Tags: #public #python3 #stdlib #enum
Import
import enum
from enum import StrEnum
Signature & Overview
enum.StrEnum(new_class_name, /, names, *, module=None, qualname=None, type=None, start=1, boundary=None)
Enum where members are also (and must be) strings
Methods & Attributes
StrEnum.capitalize(self, /)— Return a capitalized version of the string.StrEnum.casefold(self, /)— Return a version of the string suitable for caseless comparisons.StrEnum.center(self, width, fillchar=' ', /)— Return a centered string of length width.StrEnum.count(...)— Return the number of non-overlapping occurrences of substring sub in string S[start:end].StrEnum.encode(self, /, encoding='utf-8', errors='strict')— Encode the string using the codec registered for encoding.StrEnum.endswith(...)— Return True if the string ends with the specified suffix, False otherwise.StrEnum.expandtabs(self, /, tabsize=8)— Return a copy where all tab characters are expanded using spaces.StrEnum.find(...)— Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].StrEnum.format(self, /, *args, **kwargs)— Return a formatted version of the string, using substitutions from args and kwargs.StrEnum.format_map(self, mapping, /)— Return a formatted version of the string, using substitutions from mapping.StrEnum.index(...)— Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].StrEnum.isalnum(self, /)— Return True if the string is an alpha-numeric string, False otherwise.StrEnum.isalpha(self, /)— Return True if the string is an alphabetic string, False otherwise.StrEnum.isascii(self, /)— Return True if all characters in the string are ASCII, False otherwise.StrEnum.isdecimal(self, /)— Return True if the string is a decimal string, False otherwise.StrEnum.isdigit(self, /)— Return True if the string is a digit string, False otherwise.StrEnum.isidentifier(self, /)— Return True if the string is a valid Python identifier, False otherwise.StrEnum.islower(self, /)— Return True if the string is a lowercase string, False otherwise.StrEnum.isnumeric(self, /)— Return True if the string is a numeric string, False otherwise.StrEnum.isprintable(self, /)— Return True if all characters in the string are printable, False otherwise.StrEnum.isspace(self, /)— Return True if the string is a whitespace string, False otherwise.StrEnum.istitle(self, /)— Return True if the string is a title-cased string, False otherwise.StrEnum.isupper(self, /)— Return True if the string is an uppercase string, False otherwise.StrEnum.join(self, iterable, /)— Concatenate any number of strings.StrEnum.ljust(self, width, fillchar=' ', /)— Return a left-justified string of length width.StrEnum.lower(self, /)— Return a copy of the string converted to lowercase.StrEnum.lstrip(self, chars=None, /)— Return a copy of the string with leading whitespace removed.StrEnum.maketrans(...)— Return a translation table usable for str.translate().StrEnum.partition(self, sep, /)— Partition the string into three parts using the given separator.StrEnum.removeprefix(self, prefix, /)— Return a str with the given prefix string removed if present.StrEnum.removesuffix(self, suffix, /)— Return a str with the given suffix string removed if present.StrEnum.replace(self, old, new, /, count=-1)— Return a copy with all occurrences of substring old replaced by new.StrEnum.rfind(...)— Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].StrEnum.rindex(...)— Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].StrEnum.rjust(self, width, fillchar=' ', /)— Return a right-justified string of length width.StrEnum.rpartition(self, sep, /)— Partition the string into three parts using the given separator.StrEnum.rsplit(self, /, sep=None, maxsplit=-1)— Return a list of the substrings in the string, using sep as the separator string.StrEnum.rstrip(self, chars=None, /)— Return a copy of the string with trailing whitespace removed.StrEnum.split(self, /, sep=None, maxsplit=-1)— Return a list of the substrings in the string, using sep as the separator string.StrEnum.splitlines(self, /, keepends=False)— Return a list of the lines in the string, breaking at line boundaries.
Usage
from enum import StrEnum, auto
class Env(StrEnum):
DEV = auto() # "dev"
PROD = auto() # "prod"
assert Env.DEV == "dev" and f"env={Env.PROD}" == "env=prod"
Gotchas
- Requires Python 3.11+:
auto()automatically produces the lowercase member name, andstr(Env.PROD)is"prod"(unlikeclass Env(str, Enum)in Python 3.11+ wherestr()changed). - JSON-serializable out of the box:
json.dumps({"env": Env.PROD})works with zero custom encoders.
REPL Inspect
from enum import StrEnum
help(StrEnum)
Linked from
Related notes
- tuts/python3/stdlib_module/enum/ReprEnum Linked here; links to this page; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/unique Linked here; links to this page; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/auto Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/Enum Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumCheck Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumDict Linked here; shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/enum/EnumMeta shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/EnumType shared tags: #enum, #stdlib
- tuts/python3/stdlib_module/enum/Flag shared tags: #enum, #stdlib