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.
# Capitalize
a = "name"
print(a.capitalize())
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)
text = "hello world, hello universe"
count_hello = text.count("hello")
print(count_hello)
text = "hello world, hello universe"
count_hello = text.count("l")
print(count_hello)
Len() is the pre-defined function that can be used to find the length of the string.