Remove Substring from String in Python

Use replace() Method

To eliminate a substring from string in Python:

  • Define and initiate my_string.
  • Declare substring to remove it from my_string.
  • Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "")

The code above will print the following output on the console:

We tried removing the substring from my_string.

The replace() is a built-in method in Python that holds two or more parameters and a __count of the occurrences of a substring.

If specified, at most __count substitutions are performed from left to right. Finally, the method replaces all the occurrences of the substring old with new and returns a copy.

We used replace(substring, "") on my_string as:

  • __count by default is -1, but you can set it using replace(my_string, "", 2).
  • The __old string is the substring.
  • The __new string "" is nothing but an empty string.

F-strings, also known as "formatted string literals", is a new way in Python to format strings. We used it to provide a better display of information as f'Input: {my_string}'.

Use the split() Method with for loop

To get rid of a substring from a string in Python:

  • Declare an empty string my_new_string = ""
  • Use split() to split my_string by substring into array str_arr.
  • Use the for loop to iterate over each element of str_arr
  • Concatenate every element of my_string with my_new_string

The code above will print the following output on the console:

Python String Concatenation requires declared and initialized strings. my_new_string is "" that is just an empty string.

We used split() to discard the substring from my_string.

The split() method performed as:

  • Took sep as a delimiter.
  • Took maxsplit (default is -1).maxsplit defines number of maxium split.
  • Discarded maxsplit occurrences of sep from the resultant string.
  • Split the string at maxsplit points.
  • Returned a copy of a list of strings(s).

We used a for loop to iterate over all the elements of the my_string.

We used += to join the String.

In Python, we can perform String Concatenation using +=, holding the resultant string on the left side as my_new_string = my_string.

If you want to remove only first occurrence of substring, then you can use maxsplit as 1 while using split() and it won’t remove other occurrences of the substring.

The code above will print the following output on the console:

If you want to remove all occurrences of the substring, don’t pass maxsplit parameter as it is defaulted to -1 and that means there won’t be any limit to number of splits.

Use split() with join() Method

To eliminate a substring from a string in Python:

  • Use split() to remove the substring from my_string
  • Use the join() method to join every element of the list my_string

The code above will print the following output on the console:

We covered split() while explaining the code using the split() method.

We used the join() method in the above code snippet. It took an iterable object of strings to concatenate them where the delimited string is the one that provides that method. For example, we concatenated the elements of my_string using the delimiter "".

Use re.sub() Method

To eliminate a substring from a string in Python:

  • Import re
  • Use re.sub() to remove the substring from my_string

The code above will print the following output on the console:

We imported re library that provides regular expression-matching operations

The re.sub() is a method of the re library that substitutes occurrences of a pattern found in a string. It held:

  • The original string.
  • A replaceable string pattern.
  • A count (default is 0).
  • A repl string to replace the count times pattern.

You may wonder why we write the pattern as \W\WJC\W because the re library cannot nest special characters directly; we replaced each with the escape sequence \W.

We used re.sub() to replace the count times pattern of my_String with "". The count by default is 0 and it will replace all the occurrences but you can set it using count=1,2,3,....

That’s all about how to remove substring from String in Python.

Was this post helpful?

Leave a Reply

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