Remove all apostrophes from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove all apostrophes from a String in Python #

Use the str.replace() method to remove all apostrophes from a string, e.g. result = my_str.replace("'", ''). The str.replace() method will remove all apostrophes from the string by replacing them with empty strings.

main.py
my_str = 'one "two" \'three\' four "five"' # ✅ remove single quotes from string result_1 = my_str.replace("'", '') print(result_1) # 👉️ one "two" three four "five" # ✅ remove double quotes from string result_2 = my_str.replace('"', '') print(result_2) # 👉️ one two 'three' four five # ✅ remove single and double quotes from string result_3 = my_str.replace('"', '').replace("'", '') print(result_3) # 👉️ one two three four five

We used the str.replace() method to remove all apostrophes from a string.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

The first example shows how to remove all single quotes from a string.

main.py
my_str = 'one "two" \'three\' four "five"' result_1 = my_str.replace("'", '') print(result_1) # 👉️ one "two" three four "five"
Make sure to alternate between double and single quotes in the first argument to the replace() method.

We remove all single quotes from the string by replacing each occurrence with an empty string.

If you need to remove all double quotes from the string, pass a double quote as the first argument to the replace() method.

main.py
my_str = 'one "two" \'three\' four "five"' result_2 = my_str.replace('"', '') print(result_2) # 👉️ one two 'three' four five

If you need to remove all single and double quotes from the string, call the replace() method twice.

main.py
my_str = 'one "two" \'three\' four "five"' result_3 = my_str.replace('"', '').replace("'", '') print(result_3) # 👉️ one two three four five
The first call to the replace() method removes all double quotes from the string and the second call removes all single quotes.

Alternatively, you can use a generator expression.

To remove all apostrophes from a string:

  1. Use a generator expression to iterate over the string.
  2. Filter out all apostrophes.
  3. Use the str.join() method to join the remaining characters.
main.py
my_str = 'one "two" \'three\' four "five"' result = ''.join(char for char in my_str if char != '"') print(result) # 👉️ one two 'three' four five

We used a generator expression to iterate over the string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we check if the current character is not equal to a double quote and return the result.

The last step is to use the str.join() method to join the remaining characters into a string.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

For our purposes, we called the join() method on an empty string to join the remaining characters without a separator.

You can also use this approach to remove the single and double quotes from the string.

main.py
my_str = 'one "two" \'three\' four "five"' result = ''.join( char for char in my_str if char != '"' and char != "'" ) print(result) # 👉️ one two three four five

We used the and boolean operator, so for the character to be added to the generator object, both conditions have to be met.

Once we filter out the single and double quotes from the string, we join the remaining characters.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.