How to Remove Parentheses from a String in Python?

Estimated read time 1 min read

You can remove parentheses from a string in Python using the replace() method. Here’s an example:

s = 'Hello (world)!'
new_s = s.replace('(', '').replace(')', '')
print(new_s) # Output: 'Hello world!'

In this example, we have a string (s) that contains parentheses around a word. We want to remove the parentheses from the string. We achieve this by using the replace() method to replace each opening and closing parenthesis with an empty string. The resulting string (new_s) contains the original string without the parentheses.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply