The term substring refers to a sequence of characters that form a contiguous part of a string. For example, the string “Python Guide” contains the substring “Python.” Substrings can be of any length, from a single character to the entire string.
The purpose of this post is to provide an in-depth guide to Python substring by using numerous examples. The contents are as follows:
- Extract Substrings
- Modify Substrings
- Check the Presence of a Substring in a String
How to Extract Substrings in Python?
To extract a substring from the string, various methods, such as string slicing, find(), index(), string indexing, etc., are used in Python. Here are some examples that will help you understand all of these methods:
Example 1: String Slicing Method in Python
Python offers many ways to extract substrings from a string. One of the ways is called “slicing”. We can use the slice notation [start:end] to extract a substring from an input string. Here is the syntax:
string[start:end:step]
- In the above syntax, the “start:” indicates the starting index of the substring. The character at this index is included in the substring. If the start is not included, it is assumed to be 0.
- The parameter “end:” indicates the ending index of the particular substring. The character at this index is not part of the substring. If the end is not included, the string’s length is assumed.
- A “step:” parameter specifies the number of characters to skip before taking the next one. The step is assumed to be one if it is not included.
Let’s understand this method with the following example code:
Code:
string_value = "Python Guide"
substring = string_value[7:12]
print(substring)
- The string named “string_value” is initialized.
- The “string slicing” method extracts the specified string from the given string.
Output:
The specified substring has been extracted.
Example 2: Using the find() Method
The “find()” method is utilized for finding the index of a specified substring in a given string. Here is the syntax for the method:
string.find(substring, start, end)
In the above syntax, “substring” is the substring we want to find the index, and “start” and “end” is the optional starting and ending index of the search. Let’s use this method to find the substring using the below example code:
Code:
string_value = "Python Guide"
index_value = string_value.find("Guide")
print(index_value)
substring = string_value[index_value:index_value+5]
print(substring)
- The “find()” method takes the substring as an argument and finds the index of that specified substring in the given string.
- The string-slicing approach uses the index position of the substring and returns the value of the substring from the string.
Output:
The index position and substring have been extracted from the string.
Example 3: Using the index() Method
The “index()” method finds the index of a particular substring within an input string. Here is an example code:
Code:
string_value = "Python Guide"
index_value = string_value.index("Guide")
print(index_value)
substring = string_value[index_value:index_value+5]
print(substring)
- The “index()” method takes the substring as an argument and returns the index value of the substring from the input string.
Output:
The index position of the string has been extracted along with the substring.
Example 4: String Indexing in Python
The concept of string indexing is used in Python to access individual characters within a string based on their position or index number. Strings contain unique indexes for each character, ranging from 0 for the first character to n-1 for the nth, where n is the length.
Here is an example of positive string indexing in Python:
Code: (Using Positive Indexing)
string_value = "Python Guide"
substring_value = string_value[7:12]
print(substring_value)
The positive index of the specified substring is used to get that substring from the input string.
Output:
The specified substring has been obtained using the index value.
Negative indexing can also access characters at the end of a string. As an example, we can use index -1 to access the last character in the string:
Code: (Using Negative Indexing)
string_value = "Python Guide#"
substring_value = string_value[-6:-1]
print(substring_value)
- The negative indexing is used to get the substring from the end side of the string.
- The negative indexing slices the string from index -6 to index -1 (excluding -1).
Output:
The substring has been fetched using negative indexing.
How to Modify Substrings in Python?
A substring can also be modified by replacing, inserting, or deleting characters in addition to being extracted. Let’s modify the substring using these methods:
Example 1: Replace a Substring From the String
The following code uses the “replace()” function to replace the substring in the string:
Code:
string_value = "Python Guide"
print('Original String', string_value)
substring = string_value.replace("Python", "Java")
print('After Replacement: ', substring)
The “string.replace()” function is used to replace the specified substring from the targeted substring in the given string.
Output:
The specified substring has been replaced by the given substring.
Example 2: Insert a Substring From the Input String
The below code is utilized to insert the substring from the given string:
Code:
string_value = "Hello "
sub_string = "Python"
index = 6
new_string_value = string_value[:index] + sub_string + string_value[index:]
print(new_string_value)
- The new string is created by taking the first 6 characters of the first string using the slicing method.
- The new string is added to the second string (sub_string).
- After that, the string containing the first 6 characters and the substring is added to the left characters of the first created string.
Output:
The new substring has been added to the given string.
Example 3: Removing Substring From the String
The below code is used to remove a substring from the string:
Code:
string_value = "Python Guide"
substring = "Guide"
output = string_value.replace(substring, "")
print(output)
- The string and substring are initialized.
- The “string.replace()” function removes the substring “Guide” from the input string “Python Guide”.
Output:
The substring has been removed from the string.
How to Check the Presence of a Substring in a Given String?
To check the presence of a substring in a string, the “find()” method is used in Python. The “find()” method returns the index of the first appearance of a substring in the string. This function will retrieve -1 if the specified substring is not located/found. Here is an example code:
Code:
string_value = "Python Guide"
substring = "Python"
if string_value.find(substring) != -1:
print("Substring is Present!")
else:
print("Substring is Not Present!")
- The string and substring are initialized.
- The “find()” method is used to check/verify if the substring is present/exists in the string.
- If the index returned by the “find()” method is not “-1” then the substring is present in the string.
Output:
The specified substring is present in the string.
Conclusion
A substring is part of a string that is composed within another string. To manipulate strings, the substring operations, such as extracting parts of a string, checking if a string contains a certain substring, replacing, removing, inserting substrings with other strings, etc., are used in Python. To perform these operations, various functions such as replace(), string concatenation, string slicing, find(), etc., are used in Python. The guide explained Python substrings in-depth with examples to illustrate the topic.