I have the following code:
url = 'abcdc.com' print(url.strip('.com'))
Now I do
Is there a better way?
strip doesn't mean "remove this substring".
y as a set of characters and strips any characters in that set from the ends of
Instead, you could use
endswith and slicing:
url = 'abcdc.com' if url.endswith('.com'): url = url[:-4]
Or using regular expressions:
import re url = 'abcdc.com' url = re.sub('\.com$', '', url)
If you are sure that the string only appears at the end, then the simplest way would be to use 'replace':
url = 'abcdc.com' print(url.replace('.com',''))