1. Classification Methods
These methods are used to check the characteristics of individual characters in a string.
1.1 Isalpha
Syntax:
str_var.isalpha()
Gives
True
if all the characters are alphabets. Otherwise,
False
Example 1:
Code
Output
Example 2:
Code
Output
1.2 Isdecimal
Syntax:
str_var.isdecimal()
Gives
True
if all the characters are decimals. Otherwise,
False
Example 1:
Code
Output
Example 2:
Code
Output
1.3 Islower
Syntax:
str_var.islower()
Gives
True
if all letters in the string are in lowercase. Otherwise,
False
Example 1:
Code
Output
Example 2:
Code
Output
1.4 Isupper
Syntax:
str_var.isupper()
Gives
True
if all letters in the string are in uppercase. Otherwise,
False
Example 1:
Code
Output
Example 2:
Code
Output
1.5 Isalnum
Syntax:
str_var.isalnum()
Gives
True
if the string is alphanumeric (a letter or a number). Otherwise,
False
Example 1:
Code
Output
Example 2:
Code
Output
Example 3:
Code
Output
2. Case Conversion Methods
These methods are used to change the case of a string.
2.1 Capitalize
Syntax:
str_var.capitalize()
Gives a new string after converting the first letter in the string to uppercase and all other letters to lowercase.
Code
Output
2.2 Title
Syntax:
str_var.title()
Gives a new string after converting the first letter of every word to uppercase.
If a word contains a number or a special character, the first letter after that is converted to uppercase.
Example 1:
Code
Output
Example 2:
Code
Output
2.3 Swapcase
Syntax:
str_var.swapcase()
Gives a new string after converting the uppercase letters to lowercase and vice-versa.
Code
Output
3. Counting and Searching Methods
These methods are used to count the occurrences of a substring in a string and to find the position of a substring in a string.
3.1 Count
Syntax:
str_var.count(str, start_index, end_index)
Here, the
start_index
and the
end_index
are optional.
The
count()
method gives the number of times the specified string
str
appears in the string. It searches the complete string as default.
If
start_index
and
end_index
are provided, it searches between these indices. The
end_index
is not included.
Example 1:
Code
Output
Example 2:
Code
Output
3.2 Index
Syntax:
str_var.index(str, start_index, end_index)
Here, the
start_index
and the
end_index
are optional.
The
index()
method gives the index at the first occurrence of the specified string
str
.
It results in an error if the specified string
str
is not found.
The
index()
method searches the complete string as default. If
start_index
and
end_index
are provided, it searches between these indices. The
end_index
is not included.
Example 1:
Code
Output
Example 2:
Code
Output
Example 3:
Code
Output
3.3 rIndex
Syntax:
str_var.rindex(str, start_index, end_index)
Here, the
start_index
and the
end_index
are optional.
The
rindex()
method gives the index at the last occurrence of the specified string
str
.
It results in an error if the specified string
str
is not found.
The
rindex()
method searches the complete string as default. If
start_index
and
end_index
are provided, it searches between these indices. The
end_index
is not included.
Example 1:
Code
Output
Example 2:
Code
Output
Example 3:
Code
Output
3.4 Find
Syntax:
str_var.find(str, start_index, end_index)
Here, the
start_index
and the
end_index
are optional.
The
find()
method gives the index at the first occurrence of the specified string
str
.
If the specified string
str
is not found, it returns
-1
.
The
find()
method searches the complete string as default. If
start_index
and
end_index
are provided, it searches between these indices. The
end_index
is not included.
It works similarly to the
index()
method. The only difference is that the
index()
method results in an error if the specified string is not found, while
find()
does not.
Example 1:
Code
Output
Example 2:
Code
Output
Example 3:
Code
Output
3.5 rFind
Syntax:
str_var.rfind(str, start_index, end_index)
Here, the
start_index
and the
end_index
are optional.
The
rfind()
method gives the index at the last occurrence of the specified string
str
.
If the specified string
str
is not found, it returns
-1
.
The
rfind()
method searches the complete string as default. If
start_index
and
end_index
are provided, it searches between these indices. The
end_index
is not included.
It works similarly to the
rindex()
method. The only difference is that the
rindex()
method results in an error if the specified string is not found, while
rfind()
does not.
Example 1:
Code
Output
Example 2:
Code
Output
Example 3:
Code
Output