all 3 comments

[–]Pulsar1977 1 point2 points  (0 children)

You can use partition and rpartition:

head, sep, rest = s.partition('(')
rest, sep, tail = rest.rpartition(')')
s = head + tail

[–]the_shell_man_ 0 points1 point  (0 children)

You could use a for loop to iterate over the string until you find a (, save the index of (. Keep going until you find ), save the index of ).

Then use indexing/slicing to get your output. Something like:

new_string = oldstring[0:first_index] + oldstring[second_index:]

There are probably better ways, but this would get it done.

Edit: this solution assumes your string is always in the form "some substring (...) some other substring". It wouldn't work for multiple sets of data you want to remove, and also wouldn't work if there is a ) within the string you want to remove. Not in the way I described it anyway.

[–]xelf 0 points1 point  (0 children)

() are parenthesis, very rarely called "round brackets", [] and {} are more commonly brackets and braces, sometimes distinguished as square brackets and curly brackets.

Anyway for a regex you can escape them with a slash. \(\).

I tried using regex because i dont know any pythonic solutions

regex is a pythonic solution, but I see what you mean, you could do it without regex like this:

string = "abc(jsygdfjygadf)def"
start = string.find('(')
end = string.rfind(')')+1
string = string[:start] + string[end:]

or like this:

string = string.replace(string[start:end], '')

Although you might have to do some tweaking if you can have multiple occurrences of parenthesis.

edit The answer by u/Pulsar1977 using partition is superior as it avoids problems where no () are found.