1

So I have this string, and I'm iterating over a list of substrings to remove. In Ruby, where strings are mutable, I would just be able to keep changing the original string in place. But I'm running into issues figuring this out due to strings being immutable in Python.

If this is my string, and I'm trying to remove substrings in the following list (times):

string = "Play soccer tomorrow from 2pm to 3pm @homies"
times = ['tomorrow', 'from 2pm to 3pm']

How do I get this string as the desired return?

removed_times = "Play soccer @homies"

Edit: different from suggested questions b/c of multiple substrings

Hojung Kim
  • 143
  • 1
  • 2
  • 13
  • 3
    What is the expected behaviour if you want to remove the substrings `a` and `bb` from the string `cbabc`? After removing `a`, the two `b`s will be adjacent, but perhaps you don't want to remove them because `bb` was not a substring of the original string. – kaya3 Jan 15 '20 at 02:06
  • Ah, for this example specifically I'm getting a JSON using sutime (Stanford's NLP date/time parsing library) that returns parts of the text that correspond to date/time info. So the substrings will always be contained as they are in the text. – Hojung Kim Jan 15 '20 at 02:16
  • Does this answer your question? [How to delete a character from a string using Python](https://stackoverflow.com/questions/3559559/how-to-delete-a-character-from-a-string-using-python) – craq Jan 15 '20 at 03:04

3 Answers3

5

You could just use str.replace() to replace the substrings with "". This also means that the final result would need to be split and joined by " " to only have one whitespace between the words after replacing. You can use str.split() and str.join() for this.

string = "Play soccer tomorrow from 2pm to 3pm @homies"

times = ["tomorrow", "from 2pm to 3pm"]

for time in times:
    string = string.replace(time, "")

print(" ".join(string.split()))
# Play soccer @homies

Note: Strings are immutable in python, so you cannot simply modify it in-place with string.replace(time, ""). You need to reassign the string with string = string.replace(time, "").

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
2

You could split the string on the substrings, and then join it back together:

string = "Play soccer tomorrow from 2pm to 3pm @homies"
times = ['tomorrow', 'from 2pm to 3pm']
for t in times:
    string = ''.join(string.split(t))

Note that this returns the string 'Play soccer @homies' as spaces aren't caught and removed in your times.

Oliver.R
  • 1,282
  • 7
  • 17
  • why `split`/`join`? there's already a `.replace` method for this. – juanpa.arrivillaga Jan 15 '20 at 02:10
  • I evidently hadn't considered all options - `replace()`, as per RoadRunners answer, is definitely a better alternative (but that wasn't there yet when I started typing mine). – Oliver.R Jan 15 '20 at 02:13
0
string = "Play soccer tomorrow from 2pm to 3pm @homies"
times = ['tomorrow', 'from 2pm to 3pm']
removed_times = string
for x in times:
    removed_times = removed_times.replace(' ' + x + ' ', ' ')
print(removed_times)
#Play soccer @homies
Kiran Indukuri
  • 402
  • 3
  • 9