Warning: session_start(): open(/tmp/sess_93ae5d4b27dbdfefb333815358c9ba85, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Find Index Of First Occurrence Of Substring In Python - LearnShareIT

Find Index Of First Occurrence Of Substring In Python

Find index of first occurrence of substring in Python

If you are looking for an instruction to find index of first occurrence of substring in Python, keep reading our article. We will show you how to get the position of the sub-string through detailed explanations and code implementations.

Ways to find index of first occurrence of substring in Python

Using string find() function 

The function find() is built to support variables belonging to the class string. This function enables finding the first occurrence of any substring of the string if it exists. The result returns -1 and otherwise. 

Syntax:

origin_string.find(sub_string, start, end)

Parameters:

  • origin_string: The targeted string.
  • sub_string: The substring to look up.
  • start: Index to start searching. 
  • end: Index to finish searching.

Code:

myString = "Welcome to Learn Share IT"
subString = 'Learn' 
print("The position of the substring is:", end = " ")

# Use the function find() to get the position of the substring
print(myString.find(subString))

Result:

The position of the substring is: 11

Using string index() function

The function index() is almost similar to the function find(), but the function raises an exception if the substring does not exist in the string.

Syntax:

origin_string.index(sub_string, start, end)

Parameters:

  • origin_string: The targeted string.
  • sub_string: The substring to look up.
  • start: Index to start searching. 
  • end: Index to finish searching.

Code:

myString = "Welcome to Learn Share IT"
subString = 'Learn'
print("The position of the substring is:", end = " ")

# Use the function index() to get the position of the substring
print(myString.index(subString))

Result:

The position of the substring is: 11

Look at another example to see the difference between the function find() and index().

Code:

myString = "Welcome to Learn Share IT"
subString = 'Hello'
 
print("The position of the substring when using the find() function:", end = " ")
print(myString.find(subString))
 
print("The position of the substring when using the index() function:", end = " ")
print(myString.index(subString))

Result:

The position of the substring when using the find() function: -1
The position of the substring when using the index() function:
ValueError Traceback (most recent call last)
 in <module>
----> 8 print(myString.index(subString))
ValueError: substring not found

As we can see, -1 results from the find() function if the substring does not exist in the string. In case using the index function, there is an error occurring.

Using a sliding window approach

In this method, we will consider the search as a sliding window with a width equal to the length of the substring. Then we will slide it through the string to look up and compare. As soon as the string in the sliding window matches the substring, return the position of the string’s first character.

Code:

myString = "Welcome to Learn Share IT"
subString = 'Learn'
 
# Use len() to get the number of characters in the string "subString"
width = len(subString)

# Use for loop to get the position of the substring
for i in range(0, len(myString)+1 - width):
  if myString[i: i+width] == subString:
    print("The position of the substring is:", end = " ")
    print(i)

Result:

The position of the substring is: 11

Summary

Our tutorial has explained how to find index of first occurrence of substring in Python. We hope our article can help you to do that. If you have any questions, please leave us your comments. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *