Skip to content Skip to sidebar Skip to footer

How To Replace Parenthesis And Text Within It In Python String

I have two strings like this. string1 = 'Today I went to the (market) to pick up some (fruit).' string2 = 'Today I went to (school) to learn (algebra) and science.' I want to remo

Solution 1:

You could use str.replace():

string1 = "Today I went to the (market) to pick up some (fruit)."
string2 = "Today I went to (school) to learn (algebra) and science."
word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}

forword, translation in word_dict.items():  # Use word_dict.iteritems() forPython2
    string1 = string1.replace('(' + word + ')', translation)
    string2 = string2.replace('(' + word + ')', translation)

You could also use str.format() if you can control the initial strings to use {} instead of ():

string1 = "Today I went to the {market} to pick up some {fruit}."string2 = "Today I went to {school} to learn {algebra} and science."word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}

string1 = string1.format(**word_dict)
string2 = string2.format(**word_dict)

If you can't control the initial output, but would like to use str.format() anyways, you can replace any occurrences of ( and ) with { and }:

string1 = string1.replace('(', '{').replace(')', '}').format(**word_dict)
string2 = string2.replace('(', '{').replace(')', '}').format(**word_dict)

Or to do the same in a much cleaner way, you can use str.translate() along with str.maketrans():

trd = str.maketrans('()', '{}')
string1 = string1.translate(trd).format(**word_dict)
string2 = string2.translate(trd).format(**word_dict)

Keep in mind that this will replace any parenthesis with curly brackets even if they're not surrounding a word you want to replace. You could reverse translate the remaining curly brackets using rev_trd = str.maketrans('{}', '()') after you've formatted the string; but usually at that point you're better off just using the for loop and str.replace() as shown in the first code section. Unless you can change the initial strings to just contain curly brackets, then use that.

Solution 2:

You may use dict.items()

>>> string1 = "Today I went to the (market) to pick up some (fruit).">>> string2 = "Today I went to (school) to learn (algebra) and science.">>> word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'}
>>> for i,j in word_dict.items():
    string1 = string1.replace('(' + i + ')', j)
    string2 = string2.replace('(' + i + ')', j)


>>> string1
'Today I went to the library to pick up some books.'>>> string2
'Today I went to class to learn calculus and science.'

Solution 3:

A way you can do this is by splitting the string into words and whitespace, then searching for any item that begins with (. After this, simply replace the item in the list and re-concatenate the list into a string. For example.

word_dict = {'market': 'library', 'fruit': 'books', 'school': 'class', 'algebra': 'calculus'};
string1 = "Today I went to the (market) to pick up some (fruit)."
string_list = string1.split()
for item in string_list:
    if item[0] == '(':
        i = string_list.index(item)
        new_string = item[1:-1]
        string_list[i] == word_dict[new_string]
print' '.join(word[0] for word in string_list)

Best of luck and happy coding!

Post a Comment for "How To Replace Parenthesis And Text Within It In Python String"