Python

Remove Substring from String in Python

The first and foremost thing that needs to be remembered while working with python strings is that the strings in python are immutable. Immutable string means that the original string cannot be changed. To change or modify a string in python, a new string should be returned using python functions and methods. Python functions allow adding, modifying, and deleting the characters or a specific part of a string within a given string and returning a new string. There are several ways to remove a substring from a string in python; custom functions designed by the user or built-in python functions. Here are a few built-in easy-to-use python functions to help you learn how to remove a substring easily and quickly within a given string in python.

Example 1:

The replace() is a python’s built-in function that creates a new string replacing the old string or a substring in a given string. It takes a string that needs to be changed, another string that will replace the old string, and a count that tells how many occurrences of replacement to a character or a substring in a string needs to be performed. The count is optional in the replace() function; if it is not provided, only one replacement will be performed by default. Here is the composition of replace() function:

# _String.replace(Old_Str, New_Str, count)

Here is the code of replace() function:

_String = “This is a test string

Old_Str =test

New_Str =new

_String.replace(Old_Str, New_Str)

Shape Description automatically generated with medium confidence

To run the above code, a python interpreter is required. There are many python interpreters available like CPython, Jupyter, Spyder, Jython, etc. we have used Spyder version 5.2.1. The following output will be produced after executing the code given above in Spyder v5.2.1.

Here is the output.

Example 2:

The sub() python function is very much similar to the replace() function. It takes a string or a character that needs to be replaced in a string, another string or character that replaces the old string, and the original string that needs to be modified. Below is the syntax of the sub() function:

# sub(change, replaces, originalString)

The ‘change’ represents the characters or a substring in a string that needs to be changed, ‘replaces’ holds the new character or a substring to be returned, and ‘originalString’ contains the string that needs to be changed modified. The ‘replaces’ can simply be a string, or it could be a function; if it’s just a simple string, it can process any escape character, i.e., \r (carriage return), \n (newline), \j is an unknown character so it will be left alone, etc. however, if ‘replaces’ is calling a function then the function is called for each non-overlapping existence of ‘change’. A single equal object argument is provided to a function, returning the additional string. Let us see an example of sub() function:

_String = “This is! @a test #string”

Old_Str =[email protected]#”

change =[” + Old_Str + ”]

New_Str = re.sub(change,””, _String)

print(New_Str)

Shape Description automatically generated with medium confidence

The following output will be produced when the code given above is executed in the Sypder python v5.2.1.

The output of the overhead code is stated below.

Example 3:

In this example, we will eliminate a substring from a string in Python using the translate() function. The translate() is another python function that replaces the character or a substring from a string using the provided translation table. The translate() function takes a Unicode of a character that needs to be replaced in a string and a ‘None’ identifier as a replacement to remove it from the given string. The ord() python function is used to get the Unicode of a character which is provided to the translation table. Here is the syntax of translate() function:

translate({unicode: None})

Let’s run the code to execute the translate() function:

_String = “This is a test string.”

New_Str = _String.translate({ord(“i”): None})

print(New_Str)

Text Description automatically generated

After executing the above code, the following output will be produced:

Shape Description automatically generated with medium confidence

Conclusion:

This article has learned how to remove a substring from a string using the built-in python function. The first function is replaced () that takes two strings and a count; one is the old string that needs to be replaced, and the second string is the one that will replace the old string, and count tells how many occurrences of a character needs to be replaced, however, it is optional which means if it is not provided then default replacements will only be one. The second method is the sub() function.

The sub() python function is quite similar to the replace() function. It takes the three strings; the first variable holds the string that should be replaced, the second variable holds the character or a string that will replace the old string, and the original string that should be modified. Lastly, the third function is the translate() function. It is a little different from the sub() and replace() functions; however, the functionality it performs is somewhat similar to the other two functions. The translate() function takes the translation table and a ‘None’ identifier that indicates the replacement. You can easily and quickly eliminate a substring from a string in python by executing these functions.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content