Remove Single Quotes from String in Python

The article has been carefully divided into three parts, with the first part focusing on the different ways available to remove single quotes from string in Python, while the second part focusing on the different ways available to remove double quotes from string in python. The final part of the article demonstrates how to remove quotes from a string in a list in Python.

1. How to Remove Single Quotes from String in Python?

1.1 Using the replace() function

The replace() function simply substitutes one sub-string with another, both of which are taken as arguments within the function.

The single quotes can be taken as the substring to replace and it can be substituted with a simple space.

The following code uses the replace() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.2 Using a for loop with not in operator

We can use a simple for loop to individually iterate over and compare each element of the given string. The simple idea is to append all the elements into a new string until a match is found, which is then not appended into the new string.

The following code uses a for loop to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.3 Using the join() function with not in operator

The join() function joins all the given elements with the str separator into a single string. It is a string method and can easily be utilized to remove single quotes from string in Python.

Along with the join() function, we will also utilize list comprehension to implement this method.

The following code uses the join() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.4 Using the re module functions

The term re is simply an abbreviation for Regular Expression and is used to provide several functions that aid in dealing with Regular Expressions in Python. To implement the given task without any errors, we would first need to import the re library to the python code.

We will make use of the re.sub() function to remove single quotes from string in Python. We will now see how the re.sub() function is utilized to replace the \' character with a space in Python.

The following code uses the re module to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.5 Using the strip() function

The strip() function is mainly utilized to remove trailing and leading characters from a given string. Although the default leading character is a space, more leading and trailing characters can be defined as parameters of this function.

The strip() function can be utilized to remove single quotes from string in Python by simply passing the single quotes as a parameter to this function.

The following code uses the strip() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

We should note that this method removes leading and trailing characters, which means that the single quotes in between the contents of the string would not get removed by utilizing this function.

1.6 Using the lstrip()function

The lstrip() function conveniently removes any leading characters in a string. We can utilize the lstrip() function to remove any single quotes that are at the beginning of the string.

The following code uses the lstrip()function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.7 Using the rstrip() function

working similar to the lstrip() function, the rstrip() function differs in the fact that it removes trailing characters from a given string in Python. It can be utilized to remove single quotes from string in Python if these quotes exist only at the end of the string.

The following code uses the rstrip() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.8 Using the eval() function

The eval() function simply executes an argument in the form of a Python expression. If a string contains single quotes at the beginning and the end of the string, it will eliminate this extra pair of quotation marks and return a string without it.

The following code uses the eval() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

We should note that this function only works when the single quotes to be removed are at the beginning and the end of the string.

1.9 Using the ast.Literal_Eval() function

The function and working of the ast.Literal_Eval() function is the same as the eval() function explained above, with the only exception being that the former accepts only valid Python data types while the latter has no such rules.

The following code uses the ast.Literal_Eval() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

1.10 Using the translate() function

This method works for all Python 2 versions. The translate() function is utilized to return a string after replacing some of its characters mentioned in a mapping table or a dictionary.

The following code uses the translate() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

We should note that in this case, we follow the rules and syntax as allowed in the Python 2 compiler.

1.11 Using the startswith() function

While the startswith() function is usually utilized to figure out whether a string begins with some specified character or not, it can be tweaked to remove single quotes from string in Python.

The basic idea is to check whether a string starts with a single quote or not and if it does, then slice the first element of the string.

The following code uses the startswith() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

We should note that this function can only remove a leading single quote at once.

1.12 Using the endswith() function

Having a pretty similar functioning to the startswith() function explained above, the endswith() function checks whether a string ends with some specified character or not, and it can similarly be tweaked to remove trailing single quotes from string in Python.

The following code uses the endswith() function to remove single quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

2. How to remove double quotes from string in Python?

Moving on to removing double quotes from string in Python, we should note that all the methods mentioned above for removing single quotes are applicable for removing double quotes from string in Python as well. The only change that would be needed to make is to replace single quotes with double quotes in every method.

There is another method in addition to all of the above methods, which will be explained below.

  • Using the json.loads() function to remove double quotes from string in Python.

2.1 Using the json.loads() function

When a string containing double quotes is de-serialized and parsed from JSON to a Python object, the double quotes are eliminated. The json.loads() function does exactly that.

For this method to work without errors, the double quotes have to be at both ends of the string and nowhere in between.

The following code uses the json.loads() function to remove double quotes from string in Python.

The above code provides the following output:

Welcome to Java2blog

We should note that this method only works in the case of removing double quotes and provides an error if it is utilized to remove single quotes.

3. How to Remove Quotes from String in List in Python?

Moving on to the last part, we will now demonstrate the different methods available to remove quotes from a list of strings in Python.

3.1 Using the map() function with join()

The str() function simply does not remove the quotes from a string list in Python, which leads us to utilize the map() function. The map() function is basically utilized here to convert a list into a string and then utilize the join() on it.

The following code uses the map() function to remove quotes from string in list in Python.

The above code provides the following output:

[welcome, to, java2blog]

3.2 Using the translate() function

The translate() function, when using a certain translation having the value {39:None}, is able to remove quotes from a string list in Python.

The following code uses the translate() function to remove quotes from string in list in Python.

The above code provides the following output:

[welcome, to, java2blog]

3.3 Using the format() function

The format() function enables and implements string formatting in Python. This function can be tweaked and utilized along with the map() function to implement this task of removing quotes from string in list in Python.

The following code uses the format() function to remove quotes from string in list in Python.

The above code provides the following output:

[welcome , to , java2blog]

That’s all about how to remove single quotes from String in Python.

Was this post helpful?

Leave a Reply

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