On this page
collections.UserString
Prev: collections.UserList | Up: collections
Rel: collections.ChainMap, collections.Counter, collections.defaultdict, collections.deque
Ref: docs.python.org — collections.UserString
Tags: #public #python3 #stdlib #collections
Import
import collections
from collections import UserString
Signature & Overview
collections.UserString(seq)
collections.UserString class.
Methods & Attributes
UserString.capitalize(self)—UserString.capitalizemethod/property.UserString.casefold(self)—UserString.casefoldmethod/property.UserString.center(self, width, *args)—UserString.centermethod/property.UserString.count(self, sub, start=0, end=9223372036854775807)—UserString.countmethod/property.UserString.encode(self, encoding='utf-8', errors='strict')—UserString.encodemethod/property.UserString.endswith(self, suffix, start=0, end=9223372036854775807)—UserString.endswithmethod/property.UserString.expandtabs(self, tabsize=8)—UserString.expandtabsmethod/property.UserString.find(self, sub, start=0, end=9223372036854775807)—UserString.findmethod/property.UserString.format(self, /, *args, **kwds)—UserString.formatmethod/property.UserString.format_map(self, mapping)—UserString.format_mapmethod/property.UserString.index(self, sub, start=0, end=9223372036854775807)—UserString.indexmethod/property.UserString.isalnum(self)—UserString.isalnummethod/property.UserString.isalpha(self)—UserString.isalphamethod/property.UserString.isascii(self)—UserString.isasciimethod/property.UserString.isdecimal(self)—UserString.isdecimalmethod/property.UserString.isdigit(self)—UserString.isdigitmethod/property.UserString.isidentifier(self)—UserString.isidentifiermethod/property.UserString.islower(self)—UserString.islowermethod/property.UserString.isnumeric(self)—UserString.isnumericmethod/property.UserString.isprintable(self)—UserString.isprintablemethod/property.UserString.isspace(self)—UserString.isspacemethod/property.UserString.istitle(self)—UserString.istitlemethod/property.UserString.isupper(self)—UserString.isuppermethod/property.UserString.join(self, seq)—UserString.joinmethod/property.UserString.ljust(self, width, *args)—UserString.ljustmethod/property.UserString.lower(self)—UserString.lowermethod/property.UserString.lstrip(self, chars=None)—UserString.lstripmethod/property.UserString.maketrans(...)— Return a translation table usable for str.translate().UserString.partition(self, sep)—UserString.partitionmethod/property.UserString.removeprefix(self, prefix, /)—UserString.removeprefixmethod/property.UserString.removesuffix(self, suffix, /)—UserString.removesuffixmethod/property.UserString.replace(self, old, new, maxsplit=-1)—UserString.replacemethod/property.UserString.rfind(self, sub, start=0, end=9223372036854775807)—UserString.rfindmethod/property.UserString.rindex(self, sub, start=0, end=9223372036854775807)—UserString.rindexmethod/property.UserString.rjust(self, width, *args)—UserString.rjustmethod/property.UserString.rpartition(self, sep)—UserString.rpartitionmethod/property.UserString.rsplit(self, sep=None, maxsplit=-1)—UserString.rsplitmethod/property.UserString.rstrip(self, chars=None)—UserString.rstripmethod/property.UserString.split(self, sep=None, maxsplit=-1)—UserString.splitmethod/property.UserString.splitlines(self, keepends=False)—UserString.splitlinesmethod/property.
Usage
from collections import UserString
class Slug(UserString):
def __init__(self, seq) -> None:
super().__init__(str(seq).strip().lower().replace(" ", "-"))
s = Slug(" Hello World ")
assert s == "hello-world" and isinstance(s.upper(), Slug)
Gotchas
- Wraps a
strinself.data: Convenient when you want string methods (.upper(), slicing,+) to return instances of your custom subclass. - Not a built-in
str:isinstance(s, str)isFalse; convert withstr(s)when passing to C APIs that require an exactstr.
REPL Inspect
from collections import UserString
help(UserString)
Linked from
Related notes
- tuts/python3/stdlib_module/collections/UserList Linked here; links to this page; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/collections/ChainMap Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/Counter Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/defaultdict Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/deque Linked here; shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/namedtuple shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/OrderedDict shared tags: #collections, #stdlib
- tuts/python3/stdlib_module/collections/UserDict shared tags: #collections, #stdlib
- tuts/rust/std/collections shared tags: #collections, #stdlib