how do I remove the word a in this string?
We are at a boat sale near a dock.
result
We are at boat sale near dock.
I've tried:
removed = original.replace(" a", "") ,
removed = original.replace(" a ", "")
how do I remove the word a in this string?
We are at a boat sale near a dock.
result
We are at boat sale near dock.
I've tried:
removed = original.replace(" a", "") ,
removed = original.replace(" a ", "")
Looks like you just needed to replace with a space.
'We are at a boat sale near a dock.'.replace(" a ", " ")
# Result: We are at boat sale near dock. #
I'm not sure what other strings you are trying to do this with but if you can get away with it try to use string ops like this instead of regex for better performance.
a We are at a boat sale near a dock.replace("a ", "") would work. (and yes I know this would fail if a was at the end) He asked for a specific string, so I gave him an answer for that specific string.'a We are at a boat sale near a dock. a'.replace(' a ', ' ').lstrip('a ').rstrip(' a') Awaiting your answer too :)You can try by this way using regexp
import re
s= "We are at a boat sale near a dock."
op = re.sub(r'\ba\b\s+',"",s)
op
In python console
>>> import re
>>> s = 'We are at a boat sale near a dock.'
>>> op = re.sub(r'\ba\b\s+',"",s)
>>> op
'We are at boat sale near dock.'