On this page
str
Prev: staticmethod | Up: __builtins__ | Next: sum
Rel: bool, int, float, complex, bytes
Ref: docs.python.org — str
Tags: #public #python3 #builtins
Import & Availability
# Built-in (available globally without import)
from builtins import str
Overview & Signature
str(object='') -> str
str(bytes_or_buffer[, encoding[, errors]]) -> str Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer
Common Methods & Attributes
str.capitalize(self, /)— Return a capitalized version of the string.str.casefold(self, /)— Return a version of the string suitable for caseless comparisons.str.center(self, width, fillchar=' ', /)— Return a centered string of length width.str.count(...)— Return the number of non-overlapping occurrences of substring sub in string S[start:end].str.encode(self, /, encoding='utf-8', errors='strict')— Encode the string using the codec registered for encoding.str.endswith(...)— Return True if the string ends with the specified suffix, False otherwise.str.expandtabs(self, /, tabsize=8)— Return a copy where all tab characters are expanded using spaces.str.find(...)— Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].str.format(self, /, *args, **kwargs)— Return a formatted version of the string, using substitutions from args and kwargs.str.format_map(self, mapping, /)— Return a formatted version of the string, using substitutions from mapping.str.index(...)— Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end].str.isalnum(self, /)— Return True if the string is an alpha-numeric string, False otherwise.str.isalpha(self, /)— Return True if the string is an alphabetic string, False otherwise.str.isascii(self, /)— Return True if all characters in the string are ASCII, False otherwise.str.isdecimal(self, /)— Return True if the string is a decimal string, False otherwise.str.isdigit(self, /)— Return True if the string is a digit string, False otherwise.str.isidentifier(self, /)— Return True if the string is a valid Python identifier, False otherwise.str.islower(self, /)— Return True if the string is a lowercase string, False otherwise.str.isnumeric(self, /)— Return True if the string is a numeric string, False otherwise.str.isprintable(self, /)— Return True if all characters in the string are printable, False otherwise.str.isspace(self, /)— Return True if the string is a whitespace string, False otherwise.str.istitle(self, /)— Return True if the string is a title-cased string, False otherwise.str.isupper(self, /)— Return True if the string is an uppercase string, False otherwise.str.join(self, iterable, /)— Concatenate any number of strings.str.ljust(self, width, fillchar=' ', /)— Return a left-justified string of length width.str.lower(self, /)— Return a copy of the string converted to lowercase.str.lstrip(self, chars=None, /)— Return a copy of the string with leading whitespace removed.str.maketrans(...)— Return a translation table usable for str.translate().str.partition(self, sep, /)— Partition the string into three parts using the given separator.str.removeprefix(self, prefix, /)— Return a str with the given prefix string removed if present.str.removesuffix(self, suffix, /)— Return a str with the given suffix string removed if present.str.replace(self, old, new, /, count=-1)— Return a copy with all occurrences of substring old replaced by new.str.rfind(...)— Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].str.rindex(...)— Return the highest index in S where substring sub is found, such that sub is contained within S[start:end].str.rjust(self, width, fillchar=' ', /)— Return a right-justified string of length width.str.rpartition(self, sep, /)— Partition the string into three parts using the given separator.str.rsplit(self, /, sep=None, maxsplit=-1)— Return a list of the substrings in the string, using sep as the separator string.str.rstrip(self, chars=None, /)— Return a copy of the string with trailing whitespace removed.str.split(self, /, sep=None, maxsplit=-1)— Return a list of the substrings in the string, using sep as the separator string.str.splitlines(self, /, keepends=False)— Return a list of the lines in the string, breaking at line boundaries.str.startswith(...)— Return True if the string starts with the specified prefix, False otherwise.str.strip(self, chars=None, /)— Return a copy of the string with leading and trailing whitespace removed.str.swapcase(self, /)— Convert uppercase characters to lowercase and lowercase characters to uppercase.str.title(self, /)— Return a version of the string where each word is titlecased.str.translate(self, table, /)— Replace each character in the string using the given translation table.str.upper(self, /)— Return a copy of the string converted to uppercase.str.zfill(self, width, /)— Pad a numeric string with zeros on the left, to fill a field of the given width.
Usage
s = " hello, world "
assert s.strip().split(", ") == ["hello", "world"]
assert "prefix_name".removeprefix("prefix_") == "name"
Gotchas
- Immutable: Building strings with
+=in a loop is slow—accumulate parts in alistand call"".join(parts). - Use
.removeprefix()/.removesuffix()(3.9+) instead of.lstrip()/.rstrip()(which strip individual characters, not substrings!).
REPL Inspect
help(str)
Linked from
- tuts/python3
- tuts/python3/__builtins__
- tuts/python3/__builtins__/__builtins__
- tuts/python3/__builtins__/bool
- tuts/python3/__builtins__/bytearray
- tuts/python3/__builtins__/bytes
- tuts/python3/__builtins__/complex
- tuts/python3/__builtins__/dict
- tuts/python3/__builtins__/float
- tuts/python3/__builtins__/frozenset
- tuts/python3/__builtins__/int
- tuts/python3/__builtins__/list
- tuts/python3/__builtins__/memoryview
- tuts/python3/__builtins__/object
- tuts/python3/__builtins__/range
- tuts/python3/__builtins__/set
- tuts/python3/__builtins__/slice
- tuts/python3/__builtins__/staticmethod
- tuts/python3/__builtins__/sum
- tuts/python3/__builtins__/tuple
- tuts/python3/__builtins__/type
- tuts/python3/tutorials/core_fundamentals/primitive_data_types
Related notes
- tuts/python3/__builtins__/__builtins__ Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bool Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytes Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/complex Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/float Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/int Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/staticmethod Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__/sum Linked here; links to this page; shared tags: #builtins
- tuts/python3/__builtins__ links to this page; shared tags: #builtins
- tuts/python3/__builtins__/bytearray links to this page; shared tags: #builtins