In the below example, we have a list of languages and the hyphen is appended at both the trailing and leading end of each element. We can use the strip() method to remove those special characters and preserve the original value. In that case, there are higher chances that data is not formed correctly, and we may get the separators like pipe, comma, hyphen etc., appended to the string. Suppose we are fetching data from external sources like Excel, DB or third party APIs.
# Remove the whitespace and specified character onĬoding is fun Example 2 – How to use strip() method in real world? # Leading and trailing whitespaces are removed Python provides us with different ways to remove trailing characters like newlines, spaces. text4.strip('The') – Removes the substring only at the leading end. Lets take a look at how we can strip from a string in Python.text2.strip('code') – Removes only the substring from both leading and trailing end.
Remove \n From String Using str.replace() Method in Python Removing Vowel Characters from a String in Python (Video 26) Old String: