How to delete parentheses for an input in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to delete parentheses for an input in python?

I have an input like this: Input: (A - B + C) - (C / (D - E)) I need to delete parentheses and print output like this: Output: A - B + C - (C / (D - E)) Could you please help me write this program? Or give me a hint about how to solve it? Thanks 🤗🙏🙏🙏

20th Dec 2021, 5:28 AM
Saba
Saba - avatar
2 Answers
+ 6
Saba , this description works only correct for the first pair of parenthesis starting from left side: ▪︎split the input with the list constructor: inp = list("(A - B + C) - (C / (D - E))") this will separate all characters to individual list elements ▪︎then you can use the index method of list to find the first opening parenthesis "(" starting from left side this will return the index number of the mentioned character. use this index to delete the character in the list. ▪︎ do the same with the closing parenthesis as mentioned above. ▪︎the last step is to join() the modified list back to a string. if you need more help, please do a try and post it here. happy coding and good success!
20th Dec 2021, 12:11 PM
Lothar
Lothar - avatar