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?


import_contacts

You may also like:

Related Posts

  • 19 May

    Remove First and Last Character of String in Python

    Table of ContentsHow to Remove First and Last Character of string in python?Remove First and Last Character of string Using For LoopRemove First and Last Character of string Using SlicingRemove First and Last word of string in PythonConclusionWas this post helpful? We use strings in python to handle text data. For processing the data, we […]

  • 19 May

    Remove HTML Tags From String in Python

    Table of ContentsRemove HTML tags from string in python Using Regular ExpressionsRemove HTML tags from string in python Using the lxml ModuleRemove HTML tags from string in python Using the Beautifulsoup ModuleConclusionWas this post helpful? While collecting data, we often need to process texts with HTML tags. In this article, we will discuss different ways […]

  • 19 May

    Remove xa0 from String in Python

    Table of ContentsHow to remove xa0 from string in python?Using the decode() function to remove xa0 from string in python.Using the re library to remove xa0 from string in python.Using the normalize() function from the unicodedata library to remove xa0 from string in python.Using the get_text() function from the BeautifulSoup library to remove xa0 from […]

  • 18 May

    Convert bool to String in Python

    Table of ContentsUsing the str() function to convert bool to string in Python.Using string formatting with the help of the % operator to convert bool to string in Python.Using string formatting with the help of the format() function to convert bool to string in Python.Using f-strings to convert bool to string in Python.Was this post […]

  • 18 May

    Convert UUID to String in Python

    Table of ContentsUUID in PythonWays to convert UUID to string in PythonUsing the str() function to convert UUID to String in PythonUsing the hex attribute to convert UUID to String in PythonUsing the urn attribute to convert UUID to String in PythonConclusionWas this post helpful? In this post, we will see how to convert UUID […]

  • 18 May

    How to Replace Word in String in Python

    Table of ContentsUsing the replace() Function to replace word in string in PythonUsing the re.Sub() Function to replace word in string in PythonUsing the re.subn() function to replace word in string in PythonConclusionWas this post helpful? In this tutorial, we will to how to replace word in string in Python. Strings represent a sequence of […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.