Strings in Python are represented using the built-in str
type/class. This class defines methods which can be used to manipulate or extract information from string instances. The string methods can be divided into:
- Searching Methods
- Manipulation Methods
- Validation Methods
- Formatting Methods
- Encoding and Decoding Methods
Searching Methods
These methods are used to determine if a given pattern is a substring in a string. All of these methods are case sensitive.
find()
Syntax:
S.find(pattern)
This method returns the beginning index of the leftmost occurrence of the pattern in the string S.
-1 is returned if the given pattern does not occur in the string.
index()
Syntax:
S.index(pattern)
This method is similar to find()
except that, if the pattern does not occur in the string, a ValueError
is raised. It returns the starting index of the leftmost occurrence of the pattern in the string S.
rfind()
Syntax:
S.rfind(pattern)
This method returns the beginning index of the rightmost occurrence of the pattern in the string S. It returns -1 if the pattern does not occur in the string.
rindex()
Syntax:
S.rindex(pattern)
This method is similar to rfind()
, but raises a ValueError
if the pattern does not occur in the string S.
count()
Syntax:
S.count(pattern)
This method returns the number of non-overlapping times that the pattern occurs in the string S.
The Searching methods also takes two optional arguments, start and stop . These arguments effectively restricts the search to the slice S[start : stop], where S is the string. The syntax is:
S.method(pattern, start, stop)
If the stop is not given, the search goes on up to the end of the string.
Manipulation Methods
These methods are used to construct strings related to another string. Since strings are immutable, none of these method modifies the original string, they instead return an entirely new string that is closely related to the original one.
replace()
Syntax:
S.replace(old, new)
Returns a new copy of string S with all occurrences of old replaced with new .
capitalize()
Syntax:
S.capitalize()
Returns a new copy of string S with the first character being uppercase and all the subsequent alphabetical characters being lower case.
upper()
Syntax:
S.upper()
Returns a copy of string S with all the alphabetical characters being uppercase.
lower()
Syntax:
S.lower()
Returns a new copy of S with all the alphabetical characters being lowercase.
strip()
Syntax:
S.strip()
Returns a new copy of S with all the leading and trailing spaces removed.
lstrip()
Syntax:
S.lstrip()
Returns a new copy of string S with leading spaces removed.
rstrip()
Syntax:
S.rstrip()
Returns a new copy of string S with trailing spaces removed.
join()
Syntax:
(sep).join(S)
Return the composition of the given sequence of strings, inserting sep as delimiter between each pair.
The join method is also used to form a string from other compatible types which are built with strings fragments, for example to form a string from a list of characters, we can use an empty string as sep.
splitlines()
Syntax:
S.splitlines()
Return a list of substrings of S, as delimited by newlines.
split()
Syntax:
S.split(sep, count)
Return a list of substrings of S, as delimited by the first count occurrences of sep. If count is not specified, split on all occurrences. If sep is not specified, use whitespace as delimiter.
rsplit()
Syntax:
S.rsplit(sep, count)
Similar to split, but using the rightmost occurrences of sep.
partition()
Syntax:
S.partition(sep)
Returns (head, sep, tail) such that S = head + sep + tail, using leftmost occurrence of sep, if any; else return (S,'' ,'' )
rpartition()
Syntax:
S.rpartition(sep)
Return (head, sep, tail) such that S = head + sep + tail, using rightmost occurrence of sep, if any; else return ( '', '', s)
swapcase()
Syntax:
S.swapcase()
Returns a copy of the string S, but with all the upper case letters lowercased and vice versa.
Validation Methods
These methods are used to test a boolean property of a string, such as whether it begins or ends with a given pattern. These methods returns a boolean value i.e either True
or False.
startswith()
Syntax:
S.startswith(pattern)
Returns True
if the string S starts with the given pattern,. else False
.
endswith()
Syntax:
S.endswith(pattern)
Returns True
if the string S ends with the given pattern, else False
.
isspace()
Syntax:
S.isspace()
Returns True
if all the characters of a non-empty string S are white spaces, else False
.
isalpha()
Syntax:
S.isalpha()
Returns True
if all the characters of a non-empty string S are alphabetic, else False
. Alphabetic characters are uppercase A-Z and lowercase a-z.
islower()
Syntax:
S.islower()
Returns True
if the string S contains one or more alphabetic characters, all of which are lowercased .
isupper()
Syntax:
S.isupper()
Returns True
if the string S is made up of one or more alphabetic characters, all of which are uppercased.
isdigit()
Syntax
S.isdigit()
Returns True
if all the characters of string S are digits, else False
.
isnumeric()
Syntax:
S.isnumeric()
Return True
if all characters of nonempty string are numeric Unicode characters (e.g., 0–9, equivalents, fraction characters)isalnum()
Syntax:
S.isalnum()
Return True if all characters of nonempty string are either alphabetic or numeric.
isidentifier()
Syntax:
S.isidentifier()
Returns True
if the string S is a valid identifier, else False
. A string is considered a valid identifier if it only contains alphanumeric letters (a-z) and (0-9), or underscores (_). A valid identifier cannot start with a number, or contain any spaces.
istitle()
Syntax:
S.istitle()
The istitle()
method returns True if all words in a text start with a upper case letter, and the rest characters of the word are all lower case, otherwise False.
Numbers and special characters are ignored.
isprintable()
Syntax:
S.isprintable()
The isprintable()
method returns True if all the characters are printable, otherwise False.
Formatting Method
The format()
method is used in formatting a string. It is used to substitute values in a string either by position or by name.
Syntax:
S.format(*args, **kwargs)
Curly braces {} are used as placeholders inside the string to designate positions where the substitute values will be positioned.
Examples of formatting with positional arguments:
Positions can be put inside the curly braces to alter the order of substitution.
Example of formatting with keyword arguments: