Me All
Me All

Reputation: 269

Using strip() to remove only one element

I have a word within two opening and closing parenthesis, like this ((word)). I want to remove the first and the last parenthesis, so they are not duplicate, in order to obtain something like this: (word).

I have tried using strip('()') on the variable that contains ((word)). However, it removes ALL parentheses at the beginning and at the end. Result: word.

Is there a way to specify that I only want the first and last one removed?

Upvotes: 4

Views: 4733

Answers (7)

Rashid Saleem
Rashid Saleem

Reputation: 13

try this one:

str="((word))"

str[1:len(str)-1]

print (str)

And output is = (word)

Upvotes: 1

Sopan
Sopan

Reputation: 691

Try below:

>>> import re
>>> w = '((word))'
>>> re.sub(r'([()])\1+', r'\1', w)
'(word)'
>>> w = 'Hello My ((word)) into this world'
>>> re.sub(r'([()])\1+', r'\1', w)
'Hello My (word) into this world'
>>> 

Upvotes: 2

gofvonx
gofvonx

Reputation: 1439

You can use regex.

import re
word = '((word))'
re.findall('(\(?\w+\)?)', word)[0]

This only keeps one pair of brackets.

Upvotes: 3

Nordle
Nordle

Reputation: 2981

For this you could slice the string and only keep from the second character until the second to last character:

word = '((word))'
new_word = word[1:-1]
print(new_word)

Produces:

(word)

For varying quantities of parenthesis, you could count how many exist first and pass this to the slicing as such (this leaves only 1 bracket on each side, if you want to remove only the first and last bracket you can use the first suggestion);

word ='((((word))))'
quan = word.count('(')
new_word = word[quan-1:1-quan]
print(new_word)

Produces;

(word)

Upvotes: 6

Albin Paul
Albin Paul

Reputation: 3419

Well , I used regular expression for this purpose and substitute a bunch of brackets with a single one using re.sub function

import re
s="((((((word)))))))))"
t=re.sub(r"\(+","(",s)
g=re.sub(r"\)+",")",t)
print(g)

Output

(word)

Upvotes: 2

Ilia Gilmijarow
Ilia Gilmijarow

Reputation: 1020

you can replace double opening and double closing parentheses, and set the max parameter to 1 for both operations

print('((word))'.replace('((','(',1).replace('))',')',1) )

But this will not work if there are more occurrences of double closing parentheses Maybe reversing the string before replacing the closing ones will help

t= '((word))'
t = t.replace('((','(',1)
t = t[::-1]  # see string reversion topic [https://stackoverflow.com/questions/931092/reverse-a-string-in-python]
t = t.replace('))',')',1) )
t = t[::-1]  # and reverse again

Upvotes: 2

Imtinan Azhar
Imtinan Azhar

Reputation: 1753

instead use str.replace, so you would do str.replace('(','',1)

basically you would replace all '(' with '', but the third argument will only replace n instances of the specified substring (as argument 1), hence you will only replace the first '('

read the documentation :

replace(...) S.replace (old, new[, count]) -> string

Return a copy of string S with all occurrences of substring
old replaced by new.  If the optional argument count is
given, only the first count occurrences are replaced.

Upvotes: 2

Related Questions