Remove Quotation Marks from String in Python

Remove quotation marks from String in Python

Table of Contents

Quotes in Python

In Python, we can use single quotes or double quotes to define strings. A string itself may contain these quotes. Sometimes, we may have unexpected quotes within a string that needs to be dealt with.

Remove Double Quotes from String in Python

We will discuss different methods how to remove quotation marks from string in Python.

Using the replace() Function to Remove Double Quotes from String in Python

The replace() function replaces a given portion of the string with another substring. We can use this function to remove double quotes from string in Python.

For this, we will provide the double quotes as an argument in the function to get replaced by an empty string.

See the code below.

Output:

sample string

Using the re.sub() Function to Remove Double Quotes from String in Python

We can use regular expression patterns to identify and replace parts of a string using the re.sub() function. We can identify the double quotes with a regex pattern and replace them with an empty string.

See the code below.

Output:

sample string

Using the for Loop to Remove Double Quotes from String in Python

We can iterate over a string using the for loop and compare each element individually. If the element is not a double quote then we append it to a new string. This new string will not have double-quotes.

For example,

Output:

sample string

Using the join() Function to Remove Double Quotes from String in Python

The join() function combines elements from an iterable to a single string. We can use this function to remove double quotes from a string.

First, we will create a list of all characters in the string using list comprehension, comparing each character with a double quote. We will then combine the elements from this list using the join() function to return the final string.

Output:

sample string

Using the strip() Function to Remove Double Quotes from String in Python

We use the strip() function in Python to delete characters from the start or end of the string. We can use this method to remove the quotes if they exist at the start or end of the string.

See the code below.

Output:

sample string

Using the lstrip() function to remove double quotes from string in Python

The lstrip() function can remove characters from the start of the string. With this function, we can remove the double quotes from the start of the string.

For example,

Output:

sample string

Using the rstrip() Function to Remove Double Quotes from String in Python

This function is opposite to the previous method and removes characters from the end of the string. It can remove double quotes from the end of the string.

For example,

Output:

sample string

Using the startswith() Function to Remove Double Quotes from String in Python

In Python, we use the startswith() function to check whether the string starts with some character or not. We can use this function to remove double quotes from the start of the string.

We will check whether a string starts with double quotes and slice the first character if it returns True.

See the code below.

Output:

sample string

Using the endswith() Function to Remove Double Quotes from String in Python

In Python, we use the endswith() function to check whether the string terminates with some character or not. We can use this function to remove double quotes from the end of the string.

We will check whether a string ends with double quotes and slice the last character if it returns True.

See the code below.

Output:

sample string

Using the eval() Function to Remove Double Quotes from String in Python

This method works only if you have double quotes at the start and end as quotation marks.

The eval() function takes an argument and executes it as a Python expression. So, if we pass a string and it has double quotes at the start and end position, then the eval() function will return it as a normal string, removing the double quotes in the process.

For example,

Output:

sample string

However, this method is considered unsafe and should not be used for untrusted inputs.

Using the ast.Literal_Eval() Function to Remove Double Quotes from String in Python

We can use the ast.literal_eval() function similar to the previous method. This function is considered a safer alternative to the eval() function as it accepts only valid Python datatypes.

We can use it to remove double quotes from string in Python.

For example,

Output:

sample string

Using the json.Loads() Function to Remove Double Quotes from String in Python

The json.loads() function parses and deserializes the JSON string into a Python object. We can remove the extra double quotes from a string by passing them to this function.

This function also works in a similar situation as in the previous method. The double quotes should exist at the start and end of the string.

For example,

Output:

sample string

Remove Single Quotes from String in Python

All the methods discussed for removing double quotes can be used to remove single quotes also. We just have to substitute the double quotes with single quotes in the respective methods. Remember to define the string with double quotes if you want it to contain single quotes within.

We will now show examples of some methods below.

Using the join() Function to Remove Single Quotes from String in Python

For this, we will change the double quotes to single quotes in the list comprehension. We will use the join() function for the same purpose and in the same way.

See the code below.

Output:

sample string

Using the replace() Function to Remove Single Quotes from String in Python

As discussed, the replace() function can remove single quotes from string in Python by substituting the single quotes with an empty string.

For example,

Output:

sample string

The re.sub() function also works similarly but uses a regex pattern to detect the quotes.

For example,

Output:

sample string

Using the strip() Function to Remove Single Quotes from String in Python

With the strip() function, we can remove the single quotes at the start or end of the string.

For example,

Output:

sample string

Remove Quotation Marks from CSV in Python

Let us now understand how to remove quotations while reading data from CSV files.

Using the csv.reader() Function to Remove Quotation from CSV in Python.

A CSV is a simple comma-separated file. We can read CSV files using the open() function in Python. The csv.reader() file can parse the contents of the CSV file into rows.

There is a quotechar parameter in the csv.reader() function. Using this, we can specify the character used as quotes for the values in the CSV file. So they will get removed after reading the file in Python.

For example, if we are reading the following CSV file.

Then after reading the file with the above method the quotes get removed.

See the code below.

Output:

name
age
java2blog
15

We have displayed every value individually, and we can observe that no quotation can be seen.

Using the pandas.read_Csv() Function to Remove Quotation Marks from CSV in Python

Another way to do this is by reading the file into a DataFrame in Python. We will use the pandas.read_csv() function to read the file and store its contents in the DataFrame. The quotation marks will be removed this way.

For example,

name age
0 java2blog 15

Remove Quotation Marks from Data Frame in Python

A DataFrame organizes data in rows and columns. We can have columns that store string values. We will discuss how to remove the quotation marks for values in a DataFrame.

Using the pandas.str.replace() Function to Remove Quotation Marks from Data Frame in Python

The pandas.str.replace() function can replace characters from a Series object. It uses a regex expression to replace the characters.

We can remove quotation marks using this function. We will replace them with an empty string.

See the code below.

Output:

A
0 hello
1 hi
2 hey

Conclusion

In this article, we discussed how to remove quotes from string in Python. We dealt with many methods to remove double-quotes from a string in Python. All the methods can be used for single quotes as well. We also discussed how to avoid quotation marks while reading CSV files and removing them from DataFrames also.

That’s all about how to remove quotation marks from string in Python..


import_contacts

You may also like:

Related Posts

  • Remove Urls from String in Python
    03 May

    Remove Urls from Text in Python

    Table of ContentsIntroductionWays to remove URLs from Text in PythonUsing the re.sub() function to remove URLs from Text in PythonUsing the re.findall() function to remove URLs from Text in PythonUsing the re.search() function to remove URLs from Text in PythonUsing the urllib.urlparse class to remove URLs from Text in PythonConclusion In this post, we will […]

  • 16 February

    Remove Backslash from String in Python

    Table of ContentsHow to remove backslash from string in python?Using replace() Function to Remove Backslash from String in PythonUsing the decode() Function to Remove Backslash from String in PythonUsing re Library Functions to Remove Backslash from String in PythonUsing strip() Function to Remove Backslash from String in PythonHow To Remove Backslash from Json String in […]

  • Parentheses from String in Python
    08 February

    Remove Parentheses from String in Python

    Table of ContentsWays to Remove Parentheses from String in PythonUsing the replace() Function to Remove Parentheses from String in PythonUsing the re.Sub() Function to Remove Parentheses from String in PythonUsing the pandas.Str.Replace() Function to Remove Parentheses from String in PythonConclusion In this article, we will see how to remove parentheses from String in Python. Parentheses […]

  • Remove word from String in Python
    08 February

    Remove Word from String in Python

    Table of ContentsRemove Word from String in PythonHow to Remove Word from Sentence in PythonUsing the replace() functionUsing the re.sub() functionUsing the startswith() functionUsing the removeprefix() functionUsing the endswith() functionUsing the removesuffix() functionHow to Remove Duplicate Words from String in PythonUsing the set() functionUsing the set() and join() functionsUsing the join() and a user-defined functionUsing […]

  • 22 January

    Replace space with underscore in Python

    Table of ContentsWays to replace space with underscore in PythonUsing the for loopUsing the replace() functionUsing the re.sub() functionUsing the split() and join() functionConclusion Strings are an essential data type in programming. In Python, we can treat strings as an iterable of characters, and can perform a variety of functions and operations on them. Replacing […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.