How to Find First and Last Occurrence in a String
In this blog, we would discuss How to Find First and Last Occurrence in a String. Python has several in-built functions associated with the string data type. These functions can be used to perform various tasks with strings. In this article, we will learn how to find the first occurrence of a character in a string in Python.
We can use the find() method to find the first occurrence of a character in a string. The find() method returns the index of the first occurrence of the specified character. If the specified character is not found in the string, the find() method returns -1. When you need to find the last occurrence of a character or substring in a string, you can use the Python rfind() method, and also Python’s rindex function is used to find the last occurrence of a character or substring in a string.
Find the First and Last Occurrence in a String in python
When you want to find the first occurrence of a character or substring in a string, there are a few different ways you can do it in Python. If you know the character or substring you’re looking for, you can use the find() method. This method returns the index of the first occurrence of the character or substring you’re looking for
#Example: #Finding the first occurrence of 's' in the string str1 = "He sells seashells by the seashore." first_occurrence = str1.find('s') print("First occurrence of 's' in the string:", first_occurrence)
Output
The index() method returns the index of the first occurrence of the given character or substring. If the given character or substring is not found, it raises an exception. We can also use the in operator to check if a given character or substring is present in a string.
s = 'Hello World' # find the first occurrence of 'o' index = s.find('o') print(index)
Output
When you need to find the last occurrence of a character or substring in a string, you can use the Python rfind() method. This method returns the index of the last occurrence of the specified character or substring. If the character or substring is not found in the string, the find() method returns -1. For example, let’s say you have the following string:
my_string = "Hello, world!" #If you want to find the index of the last occurrence of the letter "l", you can use the find() method like this: last_index = my_string.rfind("l") print(last_index)
Output
The find() method returns the index of the last occurrence of the specified character or substring. In this case, the index of the last “l” in “Hello, world!” is 10.
Python’s rindex function is used to find the last occurrence of a character or substring. The syntax for rindex is: str.rindex(substr, start, end) where substr is the character or substring you want to find, the start is the optional starting index for the search, and end is the optional ending index for the search. If the start is not specified, the search starts at the beginning of the string. If the end is not specified, the search ends at the end of the string.
For example, let’s search for the last occurrence of the letter ‘a’ in the string ‘banana’:
fruit = 'banana' fruit.rindex('a')
Output
The search starts at the beginning of the string and returns the index of the last occurrence of ‘a’.
Also, read about difference between len() and getsizeof().
Pingback: How to Shift or Rotate an Array in Python - Study Experts