Some Useful Functions
The number of
functions (
methods of the str type) at the moment is
[
large] and
we will only present some that are particularly important for processing strings for you, as beginners.
The associated functions are used as follows:
string.function_name(parameters)
where
string is the name of the
variable, and
function_name is the name of the
method.
EXAMPLES
Run the following program:
DETAILS
capitalize() returns a string with the first letter capitalized;
lower() returns a string with all lowercase letters;
upper() returns a string with all uppercase letters;
swapcase() returns a string with all letters swapped from uppercase to lowercase and vice versa;
count(substring[, start, end]) returns the number of occurrences of a substring in the given string;
if not found, it returns
0;
find(substring[, start, end]) returns the lowest index where the substring is found in the given string;
if not found, it returns
-1. The start and end arguments are optional and specify the indices between which to search;
replace(string1, string2, [max_replacements]) returns a new string where all occurrences of
string1
are replaced with
string2. The optional argument
max_replacements allows setting a
maximum number of
replacements; if not specified, all are considered.