Remove Parentheses from String in Python

Parentheses from String in Python

In this article, we will see how to remove parentheses from String in Python.

Parentheses are normal brackets, generally used for function calling in Python. In strings, we can use them for clarity purposes like any other character. Sometimes on reading a string from unknown sources, we may find characters like this at uncanny positions.

Ways to Remove Parentheses from String in Python

We will now discuss how to remove parentheses from string in Python.

Using the replace() Function to Remove Parentheses from String in Python

In Python, we use the replace() function to replace some portion of a string with another string. We can use this function to remove parentheses from string in Python by replacing their occurrences with an empty character.

To achieve this, we will use the replace() function two times in quick succession. The first occurrence will replace the opening parentheses ( in the string with an empty character. The second usage will tackle the closing parentheses ) in the string.

For example,

Output:

remove parentheses from word

Using the re.Sub() Function to Remove Parentheses from String in Python

Regular expressions provide an efficient way to find and manipulate strings using regex patterns. In Python, we use the re module to work with regular expressions.

We can use regular expressions to remove parentheses from string in Python. For this, we will use the re.sub() function. This function substitutes the occurrence of a string that matches some pattern with another string.

We will create a pattern that will match these parentheses and replace them with an empty string.

See the code below.

Output:

remove parentheses from word

Using the pandas.Str.Replace() Function to Remove Parentheses from String in Python

A pandas DataFrame can store data in rows and columns. A column may contain rows of strings. Thus, the pandas library also has a variety of string functions to manipulate and work with such data.

The pandas.str.replace() function is used with Series objects to replace an occurrence of a substring that matches a given regular expression (regex) pattern.

We can use this function to remove parentheses from rows of a Series object at once. We can use the same pattern we did in the previous example.

For example,

Output:

A
0 hello
1 hi
2 hey

We can also use other regex patterns in this and the previous method. For example, we can use the r"\(.*\)" pattern to remove the parentheses and any string contained within the parentheses.

See the code below.

Output:

A
0
1 hi
2 he

In the above example, parentheses and everything contained within them are removed.

Conclusion

In this tutorial, we discussed how to remove parentheses from string in Python. For this, the most straightforward method is by using the replace() function to replace every occurrence of the parentheses with an empty string. We also discussed the use of regex to achieve this. For using regex to remove parentheses from string in Python, we can use the re.sub() or pandas.str.replace() function.

Thats all about how to remove parentheses from string in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *