a = 'abc\ndef' b = "ab'cd" c = """multiline string""" # Strings support slicing (negative index starts from the end): x = "hello"[2:4] # "ll" y = "hello"[1:-1] # "ell" z = "hello"[:4] # "hell"# Slice steps can be used, too: s = "hello"[::2] # "hlo" t = "hello"[3:0:-1] # "lle"
.elems()
method to iterate over their characters. Examples:"bc" in "abcd" # evaluates to True x = [c for c in "abc".elems()] # x == ["a", "b", "c"]
+
operator instead. Comparison operators perform a lexicographical comparison; use ==
to test for equality.
Members
- capitalize
- count
- elems
- endswith
- find
- format
- index
- isalnum
- isalpha
- isdigit
- islower
- isspace
- istitle
- isupper
- join
- lower
- lstrip
- partition
- removeprefix
- removesuffix
- replace
- rfind
- rindex
- rpartition
- rsplit
- rstrip
- split
- splitlines
- startswith
- strip
- title
- upper
capitalize
string string.capitalize()
count
int string.count(sub, start=0, end=None)
sub
in string, optionally restricting to [start:end]
, start
being inclusive and end
being exclusive.
Parameters
Parameter | Description |
---|---|
sub
|
string;
required The substring to count. |
start
|
int; or None ;
default is 0 Restrict to search from this position. |
end
|
int; or None ;
default is None optional position before which to restrict to search. |
elems
sequence string.elems()
[s[i] for i in range(len(s))]
, except that the returned value might not be a list.
endswith
bool string.endswith(sub, start=0, end=None)
sub
, otherwise False, optionally restricting to [start:end]
, start
being inclusive and end
being exclusive.
Parameters
Parameter | Description |
---|---|
sub
|
string; or tuple of strings;
required The suffix (or tuple of alternative suffixes) to match. |
start
|
int; or None ;
default is 0 Test beginning at this position. |
end
|
int; or None ;
default is None optional position at which to stop comparing. |
find
int string.find(sub, start=0, end=None)
sub
is found, or -1 if no such index exists, optionally restricting to [start:end]
, start
being inclusive and end
being exclusive.
Parameters
Parameter | Description |
---|---|
sub
|
string;
required The substring to find. |
start
|
int; or None ;
default is 0 Restrict to search from this position. |
end
|
int; or None ;
default is None optional position before which to restrict to search. |
format
string string.format(*args, **kwargs)
{}
. 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.# Access in order: "{} < {}".format(4, 5) == "4 < 5" # Access by position: "{1}, {0}".format(2, 1) == "1, 2" # Access by name: "x{key}x".format(key = 2) == "x2x"
Parameters
Parameter | Description |
---|---|
args
|
default is () List of arguments. |
kwargs
|
default is {} Dictionary of arguments. |
index
int string.index(sub, start=0, end=None)
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
Parameter | Description |
---|---|
sub
|
string;
required The substring to find. |
start
|
int; or None ;
default is 0 Restrict to search from this position. |
end
|
int; or None ;
default is None optional position before which to restrict to search. |
isalnum
bool string.isalnum()
isalpha
bool string.isalpha()
isdigit
bool string.isdigit()
islower
bool string.islower()
isspace
bool string.isspace()
istitle
bool string.istitle()
isupper
bool string.isupper()
join
string string.join(elements)
"|".join(["a", "b", "c"]) == "a|b|c"
Parameters
Parameter | Description |
---|---|
elements
|
required The objects to join. |
lower
string string.lower()
lstrip
string string.lstrip(chars=None)
chars
are removed. Note that chars
is not a prefix: all combinations of its value are removed:"abcba".lstrip("ba") == "cba"
Parameters
Parameter | Description |
---|---|
chars
|
string; or None ;
default is None The characters to remove, or all whitespace if None. |
partition
tuple string.partition(sep)
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
Parameter | Description |
---|---|
sep
|
string;
required The string to split on. |
removeprefix
string string.removeprefix(prefix)
prefix
, returns a new string with the prefix removed. Otherwise, returns the string.
Parameters
Parameter | Description |
---|---|
prefix
|
string;
required The prefix to remove if present. |
removesuffix
string string.removesuffix(suffix)
suffix
, returns a new string with the suffix removed. Otherwise, returns the string.
Parameters
Parameter | Description |
---|---|
suffix
|
string;
required The suffix to remove if present. |
replace
string string.replace(old, new, count=-1)
old
have been replaced with new
, optionally restricting the number of replacements to count
.
Parameters
Parameter | Description |
---|---|
old
|
string;
required The string to be replaced. |
new
|
string;
required The string to replace with. |
count
|
int;
default is -1 The maximum number of replacements. If omitted, or if the value is negative, there is no limit. |
rfind
int string.rfind(sub, start=0, end=None)
sub
is found, or -1 if no such index exists, optionally restricting to [start:end]
, start
being inclusive and end
being exclusive.
Parameters
Parameter | Description |
---|---|
sub
|
string;
required The substring to find. |
start
|
int; or None ;
default is 0 Restrict to search from this position. |
end
|
int; or None ;
default is None optional position before which to restrict to search. |
rindex
int string.rindex(sub, start=0, end=None)
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
Parameter | Description |
---|---|
sub
|
string;
required The substring to find. |
start
|
int; or None ;
default is 0 Restrict to search from this position. |
end
|
int; or None ;
default is None optional position before which to restrict to search. |
rpartition
tuple string.rpartition(sep)
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
Parameter | Description |
---|---|
sep
|
string;
required The string to split on. |
rsplit
list string.rsplit(sep, maxsplit=None)
sep
as the separator, optionally limiting the number of splits to maxsplit
. Except for splitting from the right, this method behaves like split().
Parameters
Parameter | Description |
---|---|
sep
|
string;
required The string to split on. |
maxsplit
|
int; or None ;
default is None The maximum number of splits. |
rstrip
string string.rstrip(chars=None)
chars
are removed. Note that chars
is not a suffix: all combinations of its value are removed:"abcbaa".rstrip("ab") == "abc"
Parameters
Parameter | Description |
---|---|
chars
|
string; or None ;
default is None The characters to remove, or all whitespace if None. |
split
list string.split(sep, maxsplit=None)
sep
as the separator, optionally limiting the number of splits to maxsplit
.
Parameters
Parameter | Description |
---|---|
sep
|
string;
required The string to split on. |
maxsplit
|
int; or None ;
default is None The maximum number of splits. |
splitlines
sequence string.splitlines(keepends=False)
Parameters
Parameter | Description |
---|---|
keepends
|
bool;
default is False Whether the line breaks should be included in the resulting list. |
startswith
bool string.startswith(sub, start=0, end=None)
sub
, otherwise False, optionally restricting to [start:end]
, start
being inclusive and end
being exclusive.
Parameters
Parameter | Description |
---|---|
sub
|
string; or tuple of strings;
required The prefix (or tuple of alternative prefixes) to match. |
start
|
int; or None ;
default is 0 Test beginning at this position. |
end
|
int; or None ;
default is None Stop comparing at this position. |
strip
string string.strip(chars=None)
chars
are removed. Note that chars
is neither a prefix nor a suffix: all combinations of its value are removed:"aabcbcbaa".strip("ab") == "cbc"
Parameters
Parameter | Description |
---|---|
chars
|
string; or None ;
default is None The characters to remove, or all whitespace if None. |
title
string string.title()
upper
string string.upper()