Skip to main content
A language built-in type to support strings. Examples of string literals:
Strings are not directly iterable, use the .elems() method to iterate over their characters. Examples:
Implicit concatenation of strings is not allowed; use the + operator instead. Comparison operators perform a lexicographical comparison; use == to test for equality.

Members

capitalize

Returns a copy of the string with its first character (if any) capitalized and the rest lowercased. This method does not support non-ascii characters.

count

Returns the number of (non-overlapping) occurrences of substring sub in string, optionally restricting to [start:end], start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; required
startint; or None; default is 0
endint; or None; default is None optional position before which to restrict to search.

elems

Returns an iterable value containing successive 1-element substrings of the string. Equivalent to [s[i] for i in range(len(s))], except that the returned value might not be a list.

endswith

Returns True if the string ends with sub, otherwise False, optionally restricting to [start:end], start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; or tuple of strings; required The suffix (or tuple of alternative suffixes) to match.
startint; or None; default is 0 Test beginning at this position.
endint; or None; default is None optional position at which to stop comparing.

find

Returns the first index where sub is found, or -1 if no such index exists, optionally restricting to [start:end], start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; required The substring to find.
startint; or None; default is 0 Restrict to search from this position.
endint; or None; default is None optional position before which to restrict to search.

format

Perform string interpolation. Format strings contain replacement fields surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output.If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}A replacement field can be either a name, a number, or empty. Values are converted to strings using the str function.

Parameters

ParameterDescription
argsdefault is () List of arguments.
kwargsdefault is {} Dictionary of arguments.

index

Returns the first index where sub is found, or raises an error if no such index exists, optionally restricting to [start:end]``start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; required The substring to find.
startint; or None; default is 0 Restrict to search from this position.
endint; or None; default is None optional position before which to restrict to search.

isalnum

Returns True if all characters in the string are alphanumeric ([a-zA-Z0-9]) and there is at least one character.

isalpha

Returns True if all characters in the string are alphabetic ([a-zA-Z]) and there is at least one character.

isdigit

Returns True if all characters in the string are digits ([0-9]) and there is at least one character.

islower

Returns True if all cased characters in the string are lowercase and there is at least one character.

isspace

Returns True if all characters are white space characters and the string contains at least one character.

istitle

Returns True if the string is in title case and it contains at least one character. This means that every uppercase character must follow an uncased one (e.g. whitespace) and every lowercase character must follow a cased one (e.g. uppercase or lowercase).

isupper

Returns True if all cased characters in the string are uppercase and there is at least one character.

join

Returns a string in which the string elements of the argument have been joined by this string as a separator. Example:

Parameters

ParameterDescription
elementsiterable of strings; required

lower

Returns the lower case version of this string.

lstrip

Returns a copy of the string where leading characters that appear in chars are removed. Note that chars is not a prefix: all combinations of its value are removed:

Parameters

ParameterDescription
charsstring; or None; default is None The characters to remove, or all whitespace if None.

partition

Splits the input string at the first occurrence of the separator sep and returns the resulting partition as a three-element tuple of the form (before, separator, after). If the input string does not contain the separator, partition returns (self, ”, ”).

Parameters

ParameterDescription
sepstring; required The string to split on.

removeprefix

If the string starts with prefix, returns a new string with the prefix removed. Otherwise, returns the string.

Parameters

ParameterDescription
prefixstring; required The prefix to remove if present.

removesuffix

If the string ends with suffix, returns a new string with the suffix removed. Otherwise, returns the string.

Parameters

ParameterDescription
suffixstring; required The suffix to remove if present.

replace

Returns a copy of the string in which the occurrences of old have been replaced with new, optionally restricting the number of replacements to count.

Parameters

ParameterDescription
oldstring; required The string to be replaced.
newstring; required The string to replace with.
countint; default is -1 The maximum number of replacements. If omitted, or if the value is negative, there is no limit.

rfind

Returns the last index where sub is found, or -1 if no such index exists, optionally restricting to [start:end], start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; required The substring to find.
startint; or None; default is 0 Restrict to search from this position.
endint; or None; default is None optional position before which to restrict to search.

rindex

Returns the last index where sub is found, or raises an error if no such index exists, optionally restricting to [start:end], start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; required The substring to find.
startint; or None; default is 0 Restrict to search from this position.
endint; or None; default is None optional position before which to restrict to search.

rpartition

Splits the input string at the last occurrence of the separator sep and returns the resulting partition as a three-element tuple of the form (before, separator, after). If the input string does not contain the separator, rpartition returns (”, ”, self).

Parameters

ParameterDescription
sepstring; required The string to split on.

rsplit

Returns a list of all the words in the string, using sep as the separator, optionally limiting the number of splits to maxsplit. Except for splitting from the right, this method behaves like split().

Parameters

ParameterDescription
sepstring; required The string to split on.
maxsplitint; default is unbound The maximum number of splits.

rstrip

Returns a copy of the string where trailing characters that appear in chars are removed. Note that chars is not a suffix: all combinations of its value are removed:

Parameters

ParameterDescription
charsstring; or None; default is None The characters to remove, or all whitespace if None.

split

Returns a list of all the words in the string, using sep as the separator, optionally limiting the number of splits to maxsplit.

Parameters

ParameterDescription
sepstring; required The string to split on.
maxsplitint; default is unbound The maximum number of splits.

splitlines

Splits the string at line boundaries (‘\n’, ‘\r\n’, ‘\r’) and returns the result as a new mutable list.

Parameters

ParameterDescription
keependsbool; default is False Whether the line breaks should be included in the resulting list.

startswith

Returns True if the string starts with sub, otherwise False, optionally restricting to [start:end], start being inclusive and end being exclusive.

Parameters

ParameterDescription
substring; or tuple of strings; required The prefix (or tuple of alternative prefixes) to match.
startint; or None; default is 0 Test beginning at this position.
endint; or None; default is None Stop comparing at this position.

strip

Returns a copy of the string where leading or trailing characters that appear in chars are removed. Note that chars is neither a prefix nor a suffix: all combinations of its value are removed:

Parameters

ParameterDescription
charsstring; or None; default is None The characters to remove, or all whitespace if None.

title

Converts the input string into title case, i.e. every word starts with an uppercase letter while the remaining letters are lowercase. In this context, a word means strictly a sequence of letters. This method does not support supplementary Unicode characters.

upper

Returns the upper case version of this string.