Remove the Single quotes from a String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove the Single quotes from a String in Python #

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

main.py
my_str = "One 'two' three 'four' five" result = my_str.replace("'", '') print(result) # 👉️ One two three four five

We used the str.replace() method to remove the all single quotes 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.

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

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

Alternatively, you can use a generator expression.

To remove the single quotes from a string:

  1. Use a generator expression to iterate over the string.
  2. On each iteration, check if the current character is not equal to a single quote.
  3. Use the str.join() method to join the remaining characters into a string.
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 single quote and return the result.

The output is a generator object that contains the characters of the string excluding the single quotes.

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

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

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 str.join() method on an empty string to join the remaining characters without a separator.

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.