. Advertisement .
..3..
. Advertisement .
..4..
Struggling to remove substring from string in Python? No worry, ITtutoria will provide some simple tactics for you in this regard. Overall, you can tackle the issue from three different angles. Assess each one to pick the best fit for you.
How to Remove Substring from String in Python
Method 1: Use The Replace ()
So how can we remove substring from a string? One plausible tactic is to turn to the Replace () algorithm. The Replace () receives two arguments.
The first one represents a substring to be removed from an existing main string in your Python program.
On the other hand, the second one refers to a string you use to replace the deleted substring. The illustration below will show you how this function operates.
#Define string in Python
String = "How to Remove Substring from String in Python"
#Remove substring from string using replace()
NewString = String.replace("in Python", '')
#Print result
print("The initial string is: ", String)
print("The new String is: ", NewString)
Output:
The initial string is: How to Remove Substring from String in Python
The new String is: How to Remove Substring from String
From the illustration above, you can see that it incorporates ‘deep’ (a substring) three times. You need to extract it by the Replace () function in Python. It will delete (or substitute) all substrings from your main string.
Method 2: Use The Sub ()
Sub () is pretty comparable to Replace(). It receives a substring (or a character) you wish to replace. There is also a new string to substitute the old one and the main initial string, which is expected to undergo modifications. The example below is its basic syntax:
# sub(change, replaces, originalString)
What does ‘change’ mean in this case? The term points to a substring (or characters) within the main string expected to change.
How about ‘replaces?’ It embodies the new substring (or character) that the system will return with. It can either be a simple string or serves as a function.
If the former is the case, any escapade character can undergo processes (\n (newline), \r (carriage return)). Since \j is unknown, the system will let it be.
However, there are other cases where ‘replaces’ represents a function. Hence, it will yield that function for every non-overlapping occurrence of ‘change’. A sole equilibrium object argument will be offered to the function, sending back your extra string.
In the same vein, ‘originalString’ incorporates the initial main string, whose modifications are necessary.
Let’s take a look at the complimentary example below:
import re
string1 = "How# to Remov$e Substr$ing from## Strin!g in P&ython"
old_String = "!#@$%&"
change = "[" + old_String + "]"
new_String = re.sub(change,"", string1)
print(new_String)
The subsequent output will be called upon if the codes above are processed in v5.2.1 Spyder Python.
Output:
How to Remove Substring from String in Python
Method 3: Use The Translate ()
Wrapping up our list of plausible strategies is the Translate () algorithm. It sends back a brand-new string, in which every old-string character is routed to one character of the translating table. It will then get converted into your new string. We may sum up its basic syntax as below:
str.translate(table)
Suppose we need to remove the letter ‘t’ from the phrase “How to Remove Substring from String.” Here is how to achieve that feat with Translate ():
Code:
text = 'How to Remove Substring from String'
print(text.translate({ord('t'): None}))
Output:
How o Remove Subsring from Sring
FAQs
1. Is It Possible to Convert My Main String into A Boolean?
Yes, it is possible to convert it into a Boolean. You might use bool() or json.loads () for this simple task. Tune in to this instructional article for more examples!
2. Is It Possible to Extract Only A Part of The String?
Yes, of course! The pop () function will help you in this regard. It extracts a portion of the given index and can send back that removed object if you wish to.
Conclusion
This article has shown you how to remove substring from string in Python. Each of these tactics has its distinctive edges and works well in most cases. Write to us if some of them still leave you confused; we will try our best to help alleviate your troubles!
Leave a comment