In Python, to find the first occurrence in a string of a character or substring, the easiest way is to use the Python string find() function.
string_variable = "This is a string variable we will search."
pos_of_first_a = string_variable.find("a")
pos_of_first_w = string_variable.find("w")
print(pos_of_first_a)
print(pos_of_first_w)
#Output:
8
26
You can also use the Python string index() function, but if the function doesn’t find the character or substring, it will throw an exception.
string_variable = "This is a string variable we will search."
pos_of_first_a = string_variable.index("a")
pos_of_first_w = string_variable.index("w")
print(pos_of_first_a)
print(pos_of_first_w)
#Output:
8
26
When working with string variables in Python, being able to search the string variables for substrings and characters is very useful. One such capability is to find the first or last occurrence of a substring in a string.
There are many great string methods in Python, and we can find the first occurrence of a character or substring using the Python string find() function.
The find() function, or reverse find function, returns the position of the first occurrence of a given string in a string.
If find() doesn’t find the given string, then it returns -1.
Below is some sample Python code to find the first occurrence in a string of another string.
string_variable = "This is a string variable we will search to try to find some other strings."
pos_of_first_a = string_variable.find("a")
pos_of_first_to = string_variable.find("to")
pos_of_first_string = string_variable.find("string")
pos_of_first_z = string_variable.find("z")
print(pos_of_first_a)
print(pos_of_first_to)
print(pos_of_first_string)
print(pos_of_first_z)
#Output:
8
41
10
-1
If you want to find the last occurrence in a string using Python, you can use the rfind() function.
Finding the First Occurrence in String Using Start and End Arguments With Python find() Function
With the Python find() function, we can pass optional “start” and “end” arguments to only search a specific substring of the string for the occurrence of a character or substring.
For example, if we only wanted to search the second 20 characters, we would pass “20” and “40” to find().
string_variable = "This is a string variable we will search to try to find some other strings."
pos_of_first_a = string_variable.find("a")
pos_of_first_a_first_10 = string_variable.find("a",20,40)
print(pos_of_first_a)
print(pos_of_first_a_first_10)
#Output:
8
36
Finding the First Occurrence in String of Substring Using Python index() Function
Another way we can find the first occurrence of a substring or character in a string is with the Python string index() function.
The index() function is almost the same as the find() function, but if the function doesn’t find the given substring or character, index() will throw an exception.
Below are a few examples of using the index() function in Python to the first occurrence in string of another string.
string_variable = "This is a string variable we will search."
pos_of_first_a = string_variable.index("a")
pos_of_first_w = string_variable.index("w")
print(pos_of_first_a)
print(pos_of_first_w)
#Output:
8
26
Hopefully this article has been useful for you to learn how to find the first occurrence in string of a character or substring using Python.