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..

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 25 December

    Encode String to UTF-8 in Python

    Table of ContentsEncode String to UTF-8 in PythonUsing the encode() functionUsing the codecs.encode() functionConclusion The UTF-8 encoding is used by default in Python and represents 8-bit Unicode values. The upgrade to Python 3 saw a major change in using ASCII characters to Unicode characters by default for strings. Encode String to UTF-8 in Python This […]

  • 23 December

    How To Do Line Continuation in Python

    Table of ContentsUsing Backslash (\) OperatorLine Continuation in StringLine Continuation in Number ExpressionUsing Parentheses ()Line Continuation in StringLine Continuation in Number Expression Using Backslash (\) Operator We can use \ operator for line continuation in string and number expression as follows. Line Continuation in String To do line continuation in Python Strings: Use the backslash […]

  • 16 December

    Convert List to Comma Separated String in Python

    Table of ContentsUse .join() MethodUse .join() with map() MethodUse .join() with List Comprehension Use .join() Method To convert a list to a comma separated string in Python, use the .join() method. join() method takes all elements in an iterable and joins them into one string with delimiter as separator. [crayon-63da5404f3a58158583539/] [crayon-63da5404f3a5b775631928/] First, we created a […]

  • 09 December

    Count Occurrences of Character in String in Python

    Table of ContentsUsing count() MethodUsing Naive for loopUsing List ComprehensionUsing re.findall() MethodUsing for loop with re.finditer() MethodUse the lambda Function with map()Use collections.Counter() Method Using count() Method Use String’s count() method to count occurrences of character in String in Python e.g. my_string.count(character). count() method returns number of occurrences of the character in String. [crayon-63da5404f3b5a174947131/] We […]

  • 08 December

    Find Character in String in Python

    Table of ContentsUsing find() MethodUsing index() MethodUsing for Loop with re.finditer() MethodUsing list comprehension Using find() Method To find the character in a string in Python: Use the find() method to find the index of the first occurrence of the supplied character in the input String. Use an if statement to check if the returned […]

  • 05 December

    Remove Substring from String in Python

    Table of ContentsUse replace() MethodUse the split() Method with for loopUse split() with join() MethodUse re.sub() Method Use replace() Method To eliminate a substring from string in Python: Define and initiate my_string. Declare substring to remove it from my_string. Use replace() method to remove substring from string. e.g. my_string = my_string.replace(substring, "") [crayon-63da5404f3eb3929386395/] The code […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.