A string is a sequence of Unicode characters that may contain special and alphanumeric characters. Python substring, on the other hand, is a sequence of characters that is present within another string. For instance, ‘ate’ is a substring of ‘fate’ since ‘ate’ is present in ‘fate. In this tutorial, we will be looking at substrings, how to derive substrings from a string through slicing, and how to check if a substring is present in a string.
Let’s get started.
Table of Contents
Syntax
Finding substring in python can basically be done through string slicing. You instruct python to slice a string by using the (:) sign. The syntax is generally [start:end:step]. Where start is the index you want the slicing to begin and the end is the index you want the slice to stop. Lastly, the step is the number of jumps you want to take per iteration. If it is set to 2 for instance, the index is counted in multiples of 2 i.e 0, 2, 4, 6… index. Since I want to get substrings here, the step argument won’t be defined.
Let’s see examples of how to derive substring.
How to get a substring through Slicing
- Defining both the start-index and the end-index: This is indicated as: string = string[startIndex : endIndex]
One thing to note. Remember that the index starts from 0 in python and every character counts as long as it is in the string. For example, the string ‘Python’ has 6 characters. ‘P’ takes index 0, y’ index 1 and it continues making ‘n’ index 5. 1 white space is also an index.
Let’s take a look at this code below:
#slicing using both the start-index and the end-index
string = 'Python is a good programming language' # In this string, there are 37 characters
string[10:37]
In the above example, the 10th index was the start index while the 37th index (last character) was the last index. Let’s see the result.
'a good programming language'
- Defining only the start index but not the end index: This is indicated as string = string[startIndex : ]. When the end index is not defined, it means that there is no restriction placed on the end of the string. Python, therefore, creates an output starting from the start character and ends at the last character.
#slicing using only the start-index and not the end-index
string = 'Python is a good programming language'
string[12:]
Output:
'good programming language'
- Defining only the end index but not the start index: This is indicated as string = string[ : endIndex]. This is the reverse of the last example. Python starts from the beginning of the list to the defined end index.
#slicing using only the end-index and not the start-index
string = 'Python is a good programming language'
string[:16]
Output:
'Python is a good'
- Defining neither the start nor end index: This is indicated as string = string[ : ]. When this happens, the entire string is returned.
#slicing completely
string = 'Python is a good programming language'
string[:]
Output:
'Python is a good programming language'
How to Check if Substring exists in String in Python
- Using the in operator: This is arguably the easiest method. The in operator is majorly used to check for membership in data structures. The output returns a Boolean value (that is, either True or False).
#define the string and substring
string = 'Programming'
substring = 'Program'
#use the in operator to check for membership of substring
if substring in string:
print('Substring present')
else:
print('Substring absent')
Output:
Substring present
- Using the string.find() method: Another preferable and convenient method is to use the find() method. If the character or string passed as argument is NOT in the string, it returns -1. We can build a conditional statement around this.
#define the string and substring
string = 'Programming'
substring = 'Program'
#use the string.find() method to locate the substring
if string.find(substring) == -1:
print('Substring absent')
else:
print('Substring present')
Output:
Substring present
Conclusion
In summary, we have learned what a substring is and how we can find a substring from a string. Let me know if you have any questions in the comment section. I will definitely respond to your comments.
Some FAQs
1. Using the in operation: This is perhaps the easiest method. The in operator parses the string and check if a substring exists. It returns a Boolean (i.e either True or False)
2. Using the find method: This works like the in operation. The find method takes an argument, which is the substring to be checked. It returns the index the substring begins if found and -1 if the substring is not found.
In python, the substring can be found through slicing. The syntax for slicing follows the template [start:stop]. Where start is the index of the substring’s first character and stop is the last index of the substring.