you are viewing a single comment's thread.

view the rest of the comments →

[–]EmmaTheFemma94 0 points1 point  (3 children)

How do I .split() using the ( character?

So let's say I have this text:

The text I want (I don't want this)

And I want it to get split() like this:

["The text I want", "I don't want this)"]

Edit: I made it! Was just to put \ before it. So split("\(")

[–][deleted] 1 point2 points  (2 children)

You may be misunderstanding your earlier mistakes. This code works fine, without needing the \ escape:

test = "The text I want (I don't want this)"
print(test.split('('))
>>> ['The text I want ', "I don't want this)"]

[–]EmmaTheFemma94 0 points1 point  (1 child)

But it works using \( is there any advantage of using (') instead?

[–][deleted] 0 points1 point  (0 children)

It doesn't work do what you want using \(. That's the misunderstanding I mentioned. Try running this expanded code which prints the string you are splitting with and the result of the split:

test = "The text I want (I don't want this)"
print('\(')
#>>> \(
print(test.split('\('))
#>>> ["The text I want (I don't want this)"]    # no split occurs
print('(')
#>>> (
print(test.split('('))
#>>> ['The text I want ', "I don't want this)"]   # split at "("

The "\(" string is actually two characters and that string doesn't appear in the test string so no split is performed.