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

3
  • 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 bs will be adjacent, but perhaps you don't want to remove them because bb was not a substring of the original string. Commented Jan 15, 2020 at 2: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. Commented Jan 15, 2020 at 2:16
  • Does this answer your question? How to delete a character from a string using Python Commented Jan 15, 2020 at 3:04

3 Answers 3

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, "").

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

why split/join? there's already a .replace method for this.
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).
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

2 Comments

This won't work when the substring to be removed is at the start or end of the input string; it won't have spaces on both sides in those cases.
that is a good point, the join and split is the right solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.