String Methods
In addition to Indexing and Slicing Operations, we can perform various other operations on the string object by using pre-defined functions present in the string.
The pre-defined functions present in the string are
capitalize():
capitalize()Converts the string's first character to a capital letter.
Upper is the pre-defined function. It can be used to convert enteir string into uppercase
Lower is the pre-defined function. It can be used to convert an entire string into lowercase.
swapcase is the predefined function. It can be used to convert lowercase to uppercase and uppercase to lowercase.
casefold is the predefined function. It can be used to convert uppercase to lowercase.
Strim is the predefined function. It can be used to remove the unwanted spaces between the string. It will take only one space.
Rstrip is a predefined function. It can be used to remove the right side space of the string
Lstrip is a pre-defined function. It can be used to remove the left side space of the string.
Index is a predefined function. It can be used to find specified character index positions. If the character is not there, it will show an error. If the multiple same character, it is always taken as the first character index.
Find is the predefined function. It can be used to find the specified character index position. If the character is not there it won't give any error. It will give the default values as -1.
count()
count()
function in Python is used to count the occurrences of a substring within a given string.
-
substring
: The string to be counted within the main string.
-
start
(optional): The starting index from where to begin counting.
-
end
(optional): The ending index where counting stops (not included).
Syntax : string.count(substring, start, end)
Len() is the pre-defined function that can be used to find the length of the string.
isalnum() is a predefined function. If the string contains the alphabet or numbers, or both, then it will give True. Otherwise, it shows False.
isnum() is the predefined function. If the string contains a number, then only True, otherwise False.
isalpha is the predefined function. If the string contains the alphabet then onely it is True, otherwise False.