Skip to content

Remove Substring From Python String Malli Spark By {Examples}

  • by

How to remove a substring from a string in Python? In Python, strings are immutable, meaning they cannot be modified directly. To remove a substring from a string, you need to create a new string with the desired modifications. This tutorial will guide you through the process of removing a substring from a string using various methods.

You can remove a substring from a string using many ways, for example, by using the replace(), string slicing, re.sub(), split() & join(), and strip() functions. In this article, I will explain how to remove a substring from a string by using all these methods with examples.

1. Quick Examples of Removing Substring From String

If you are in a hurry, below are some quick examples of removing substring from string.

# Quick examples of removing substring from string

import re

# Initialize the string
string = “Welcome To SparkByExamples Tutorial”

# Example 1: Using replace() method
# to remove substring from string
substr_to_remove = “Tutorial”
result = string.replace(substr_to_remove, “”)

# Example 2: Using string slicing method
# to remove substring from string
substr_to_remove = “Tutorial”
index = string.index(substr_to_remove)
result = string[:index] + string[index + len(substr_to_remove):]

# Example 3: Using re.sub() method
# to remove substring from string
substr_to_remove = “Tutorial”
result = re.sub(substr_to_remove, “”, string)

# Example 4: Using split() and join() methods
# to remove substring from string
substr_to_remove = “Tutorial”
result = “”.join(string.split(substr_to_remove))

# Example 5: Using strip() method
# to remove substring from string
substr_to_remove = “Tutorial”
result = string.strip(substr_to_remove)

2. Remove Substring from String Using replace() Method

You can use the replace() method to remove a substring from a string. For example, initialize a string string with the value “Welcome To SparkByExamples Tutorial” and then prints the original string.

First, define a variable substr_to_remove containing the substring you want to remove, which is “Tutorial” in this case. Then, the replace() method is used on the string variable. It replaces all occurrences of the substr_to_remove with an empty string “”, effectively removing the substring from the original string. The modified string is stored in the result variable.

# Initialization string
string = “Welcome To SparkByExamples Tutorial”
print(“Original string:”, string)

# Using replace() method
# Remove substring from string
substr_to_remove = “Tutorial”
result = string.replace(substr_to_remove, “”)
print(“The string after removing substring:”,result)

Yields below output.

As you can see, the substring “Tutorial” has been successfully removed from the original string.

3. Remove Substring Using String Slicing

Using string slicing you can remove the substring from the string. For example, first, initialize a string string with the value “Welcome To SparkByExamples Tutorial” and define a variable substr_to_remove containing the substring you want to remove, which is “Tutorial” in this case.

After that, find the index substr_to_remove within the string using string.index(substr_to_remove). Then, you can use string slicing to create a new string by concatenating the portion of the original string before the found index and the portion after the substring to be removed. The result is the same as using the str.replace() method to remove the specified substring from the string.

# Initialization string
string = “Welcome To SparkByExamples Tutorial”
print(“Original string:”, string)

# Using string slicing method
# Remove substring from string
substr_to_remove = “Tutorial”
index = string.index(substr_to_remove)
result = string[:index] + string[index + len(substr_to_remove):]
print(“The string after removing substring:”,result)

Yields the same output as above.

4. Remove Substring from String Using the re.sub() Method

Alternatively, you can use the re.sub() function from the re module in Python to remove a specified substring from a given string. The re.sub() function is used for performing substitutions based on regular expressions.

In the below example, the re module is imported to work with regular expressions. An initialization string is defined, string = “Welcome To SparkByExamples Tutorial”. The variable substr_to_remove is defined with the value “Tutorial”. This is the substring you want to remove from the original string. The re.sub() function is used to replace occurrences of the substr_to_remove with an empty string in the string. The result of the substitution is stored in the variable result.

import re

# Initialization string
string = “Welcome To SparkByExamples Tutorial”
print(“Original string:”, string)

# Using re.sub() method
# Remove substring from string
substr_to_remove = “Tutorial”
result = re.sub(substr_to_remove, “”, string)
print(“The string after removing substring:”, result)

Yields the same output as above.

5. Using the split() and join() Methods

You can also use the split() and join() methods to remove a substring from a string . For example, first initialize a string string with the value “Welcome To SparkByExamples Tutorial” and then prints the original string. First, define the substr_to_remove that you want to remove. Then, you can use the split() method on the string variable substr_to_remove as the delimiter. This splits the string into a list of substrings, excluding the substr_to_remove.

Next, you use the join() method to join the substrings back together without substr_to_remove, effectively removing it. The “”.join(…) call joins the list of substrings back into a single string, with an empty string as the separator. Finally, the code prints the modified string using the print() statement.

# Initialization string
string = “Welcome To SparkByExamples Tutorial”
print(“Original string:”, string)

# Using split() and join() methods
# Remove substring from string
substr_to_remove = “Tutorial”
result = “”.join(string.split(substr_to_remove))
print(“The string after removing substring:”, result)

Yields the same output as above.

6. Remove Substring from String Using strip() Method

Finally, you can use the strip() method can indeed be used to remove specific substrings from the beginning and end of a string. The strip() method in Python is primarily used to remove leading and trailing characters (whitespace by default) from a string. However, when you provide a substring as an argument to strip(), it will remove any occurrences of that substring from the beginning and end of the original string.

# Initialization string
string = “Welcome To SparkByExamples Tutorial”
print(“Original string:”, string)

# Using strip() method
# Remove substring from string
substr_to_remove = “Tutorial”
result = string.strip(substr_to_remove)
print(“The string after removing substring:”, result)

Yields the same output as above.

Conclusion

In this article, I have explained how to remove a substring from a string in Python by using replace(), string slicing, re.sub(), split() & join(), and strip() functions with examples.

Happy Learning !!

 How to remove a substring from a string in Python? In Python, strings are immutable, meaning they cannot be modified directly. To remove a substring from a string, you need to create a new string with the desired modifications. This tutorial will guide you through the process of removing a substring from a string using  Read More Python, Python Tutorial, Pending review by admin, Python String Examples 

Leave a Reply

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