HOWTO · Python

How to Remove Parentheses From String in Python

Compare four Python methods for removing literal parentheses from a string, and learn when to use each one.

On this page

To remove every literal opening and closing parenthesis from a Python string, replace each character with an empty string or keep only the characters that are not parentheses. These approaches remove the two characters themselves; they do not remove a parenthesized expression together with its contents. For example, (a + b) becomes a + b only in the sense that the parentheses disappear—the text a + b remains.

The examples below use the same input so you can compare four approaches: str.replace(), regular expressions with re.sub(), a loop, and a list comprehension with join().

Use str.replace() to Remove Parentheses From a String in Python

For this exact task, str.replace() is a direct choice. A string is immutable, so the method returns a new string and leaves the original value unchanged. Each call replaces occurrences of one substring; chain one call for ( and another for ). The optional count argument limits how many occurrences a single call replaces. Omitting it replaces every occurrence.

Run this example:

text = "(This is (a) string)"
result = text.replace("(", "").replace(")", "")
print(result)

The result contains the original text with both parenthesis characters removed. The two calls are explicit and avoid a pattern when the characters to remove are known in advance.

Use Regular Expressions to Remove Parentheses From a String in Python

Use re.sub() when the characters to remove are part of a larger pattern or when the pattern may need to grow. Import Python’s re module, pass the pattern first, the replacement second, and the input string third. In the character class [()], the parentheses are literal members, so it matches either ( or ). Replacing each match with "" removes those characters.

Run this example:

import re

text = "(This is (a) string)"
result = re.sub(r"[()]", "", text)
print(result)

For only two known literal characters, this is more machinery than chained str.replace() calls. Regular expressions are useful when a pattern makes the transformation clearer; they are not automatically the best choice for simple literal replacement.

Use a Loop to Remove Parentheses From a String in Python

A loop makes the filtering rule explicit. Start with an empty result string, examine each input character, and append it only when it is not one of the two parentheses. The input remains unchanged. This approach is easy to adapt when the condition for keeping a character depends on additional logic.

Run this example:

text = "(This is (a) string)"
result = ""
for character in text:
    if character not in "()":
        result += character
print(result)

The condition tests membership in the two-character string "()"; it excludes either parenthesis and keeps spaces and all other characters. For a short string this is straightforward. For building results from many characters, a comprehension with join() expresses the same filtering in a compact form.

Use a List Comprehension and join() to Remove Parentheses From a String in Python

A list comprehension selects the characters to keep and creates a list of them. join() then combines those characters without inserting a separator. Here, the empty string before .join() is the separator, so adjacent retained characters stay adjacent. join() is a string method called on that separator string.

Run this example:

text = "(This is (a) string)"
characters = [character for character in text if character not in "()"]
result = "".join(characters)
print(result)

Choose str.replace() for a small, fixed set of literal characters. Choose re.sub() when the rule is naturally a pattern. Use a loop when the filtering rule needs explicit control, or a list comprehension with join() for concise character filtering. All four methods remove literal parentheses and preserve their contents; parsing nested parentheses or removing balanced groups requires a different approach.