find the position of the first occurrence of a substring in a string in python
Code snippet on how to find the position of the first occurrence of a substring in a string in python
string = "I love coding"
substring = "love"
position = string.find(substring)
print("The position of "+substring+" is:", position)
This code finds the position of the first occurrence of a substring in a string. First, two strings are given as examples, “I love coding” and “love”. Then, the find() method is used to look for the first occurrence of the substring in the string and the position is stored in the variable called “position”. Finally, the position is printed out. Using this code, the position of “love” in the string “I love coding” is printed as 2.