Hello Pythonistas!
Today I will discuss an issue caused in my project and how I fixed this. We know that we have a strip method in the python string objects. When we use this method our expectation will be to remove the given sequence from the string, But actually strip remove the set of characters from the given series. I will explain this with an example
Code
a = "abc.com"
b = a.strip(".com")
print(b)
Expected Result
abc
Actual Result
ab
Why
The python documentation about strings specifies that the strip removes all the occurrences of the given set of characters. You can refer to the documentation here
Solution for this issue
If you are using python3.9 and above we have two methods removeprefix and removesuffix.
Code
a = "abc.com"
b = a.removeprefix(".com")
print(b)
Output
abc
If you are using the below that use the string slicing method or replace method
b = a[:-4]
c = a.replace(".com", "")
Hope you have learned from this. If you have faced a similar issue please feel free to share your experience with Parseltongue, Also share your feedback at afsal@parseltongue.co.in