Thus, within this guide, we will be discussing the find method to find the first occurrence in a string while coding in the Python language. Make sure to have Python 3 latest version installed on your Ubuntu 20.04 system. Let’s begin with opening the shell terminal with the keystroke “Ctrl+Alt+T”.
Example 1
Begin with the creation of a new Python file named “occur.py”. Use the “touch” keyword for this purpose. Ubuntu 20.04 comes up with many editors already configured in it. You may use the vim editor, text editor, or GNU Nano editor. Nano editor has been utilized to open the newly created file within the shell console. Both commands have been shown below.
$ nano occur.py
Within the empty file, add the python-support as highlighted in the form of red text at the top of a file. We have initialized a string variable with a string value in it. This string contains two occurrences of the alphabet “I” which we want to search for. The first print statement has been used to display the original string. Another variable “index” has been declared. The “find” function has been used to get the index of the first occurrence of an alphabet “I”. This index number will be saved to the variable “index” and the print statement will display it on the shell.
string = “I am a girl. I know programming.”
print(“Original String is: ”, string)
index = string.find(“I”)
print(“Index of occurrence ‘I’ is: ”, index)
Python3 has been used to execute the file. In return, we have got the index number for the first occurrence of an alphabet “I” as per the output i.e. 0.
Example 2
Let’s see how the find() method works on the occurrence that is not found in the string. So, we have updated the string and printed it out. After this, two print statements are using the “find()” function on the string to get the index number of the alphabet “a” and “I” separately. The alphabet “a” is already in the string but “I” is nowhere in the string.
string = “This is a string. Let’s have a look”
print(“Original String is: ”, string)
print(“Index of occurrence ‘a’ is: ”, string.find(“a”))
print(“Index of occurrence ‘I’ is: ”, string.find(“I”))
Execute the code file with the python3 keyword. In return, we have got the index of the first existence of the alphabet “a” at index 8. On the other hand, for the alphabet “I”, it returns -1 as there is no occurrence of the alphabet “I”.
Example 3
Let’s make another example with a little update. We have stated two strings s1 and s2. The variable start has been initialized with a value of 4. Two print statements are used to print the string s1 and s2 separately. The find() method has been used on variable s1 to find the substring “s1” from it while starting from the index number 4. Where the first occurrence of substring s1 i.e. “is” found, its index will be saved to the variable index. The index will be printed out.
s1 = “This is an original string.”
s2 = “is”
start = 4
print(“Original String is: ”, s1)
print(“Occurrence is: ”, s2)
index = s1.find(s2, start)
print(“Index of occurrence: ”, index)
After executing this updated code, we have found the index number of the first occurrence of the word “is” is 5 after the starting position defined in the find() method.
Conclusion
Within this guide, we have discussed the many ways to use the find() function to get the first occurrence of a specific string. We have discussed quite simple and understanding examples in Ubuntu 20.04. We believe this article will be constructive to every user.