How do you print raw strings in Python without quotes?

There are several ways you can remove quotes from a string in Python. You may need to remove all quotes or just ones surrounding a string. You may also need to remove single or double quotes.

Show

In this short article, we've compiled a comprehensive list of methods you can use to remove quotes from a string in Python. It's absolutely up to you which one you'll use. Please note that the solutions we've listed here are not ordered from best to worst. Each solution is good as long as it meets your needs in a specific case.

How to Remove All Quotes from a String in Python

First of all, let's take a look at how to remove all quotes from a string. Solutions listed in this section can be altered to work with both single and double quotation marks. But, for the purpose of this article, let's say we have an example string that has three double quotation marks in it:

example_str = '"This is a string" with quotes"'

In this section, we'll take a look at several ways how to remove all " from the example_str.

str.replace()

The first approach we'll discuss is to use the str.replace() method on the example_str. It accepts two arguments - the string we want to replace and the replacement string. In this case, we'll replace a double quotation mark (") with an empty string:

The str.replace() used in this way will remove all double quotes from the example_str.

Regular Expressions

This approach uses regular expressions to remove all quotes from a string. Before we start using regular expressions in Python, we first need to import the

import re
0 module:

import re

After that, we can use the

import re
1 method to substitute all occurrences of the quotation mark with the empty string:

This will give us the example_str without any quotes in it.

str.join()

The

import re
3 is another method we can use to remove all quotes from a string in Python. This solution has a bit more complex syntax than other solutions if you are not already familiar with the
import re
4 method. We'll essentially go over the example_str character-by-character and append each which is not a quotation mark to the empty string. That way, we'll get the example_str without any quotes in it:

How to Remove Quotes Surrounding a String

So far, we've been discussing several approaches on how to remove all quotation marks from a string in Python. That's only one way of looking at removing quotes from a string. Arguably, the more common problem is how to remove only quotes surrounding a string. The

import re
7 method is intended to do just that. Therefore, we'll discuss the
import re
7 method in this section.

Say we have the same exact example_str as in the previous section, and we want to remove only the first and the last quotation marks:

example_str = '"This is a string" with quotes"'

Using the

import re
7 with a double qoutation mark as its argument will remove leading and trailing quotation marks from the example_str:

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

Note: No matter how many leading and/or leading quotation marks are in the string, the

import re
7 will remove all of them.

Besides the usual

import re
7 method, there are also two of its derivatives -
example_str = '"This is a string" with quotes"'
4 and
example_str = '"This is a string" with quotes"'
5. The first one removes all leading quotes, and the second removes all trailing quotes:

Conclusion

In this short article, we've taken a comprehensive look at how to remove quotes from a string in Python. First of all, we've explained how to remove all quotes from a string using several different methods - str.replace(),

import re
3, and
import re
1. Afterward, we've taken a look at the
import re
7 method, as well as its derivatives
example_str = '"This is a string" with quotes"'
4 and
example_str = '"This is a string" with quotes"'
5.

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.

1

2

3

4

5

 

a1 = 'samp"le s"tring'

a2 = a1.replace('"','')

print(a2)      

 

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.

1

2

3

4

5

6

 

import re

a1 = 'samp"le s"tring'

a2 = re.sub('"','',a1)

print(a2)      

 

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,

1

2

3

4

5

6

7

8

 

a1 = 'samp"le s"tring'

a2 = ''

for i in a1:

    if(i not in '"'):

        a2 = a2 + i

print(a2)      

 

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.

1

2

3

4

5

 

a1 = 'samp"le s"tring'

a2 = ''.join(i for i in a1 if i not in '"')

print(a2)      

 

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.

1

2

3

4

5

 

a1 = '"sample string"'

a2 = a1.strip('\"')

print(a2)      

 

Output:

sample string

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

The replace()1 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,

1

2

3

4

5

 

a1 = '"sample string'

a2 = a1.lstrip('\"')

print(a2)      

 

Output:

sample string

Using the replace()3 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,

1

2

3

4

5

 

a1 = 'sample string"'

a2 = a1.rstrip('\"')

print(a2)      

 

Output:

sample string

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

In Python, we use the replace()4 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.

1

2

3

4

5

6

 

a1 = '"sample string'

if a1.startswith('"'):

    a2 = a1[1:]

print(a2)    

 

Output:

sample string

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

In Python, we use the replace()6 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.

1

2

3

4

5

6

 

a1 = 'sample string"'

if a1.endswith('"'):

    a2 = a1[:-1]

print(a2)    

 

Output:

sample string

Using the replace()8 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 replace()8 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 replace()8 function will return it as a normal string, removing the double quotes in the process.

For example,

1

2

3

4

5

 

a1 = '"sample string"'

a2 = eval(a1)

print(a2)    

 

Output:

sample string

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

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

We can use the re.sub()2 function similar to the previous method. This function is considered a safer alternative to the replace()8 function as it accepts only valid Python datatypes.

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

For example,

1

2

3

4

5

6

 

import ast

a1 = '"sample string"'

a2 = ast.literal_eval(a1)

print(a2)    

 

Output:

sample string

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

The re.sub()5 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,

1

2

3

4

5

6

 

import json

a1 = '"sample string"'

a2 = json.loads(a1)

print(a2)    

 

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.

1

2

3

4

5

 

a1 = "samp'le s'tring"

a2 = ''.join(i for i in a1 if i not in "'")

print(a2)      

 

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,

1

2

3

4

5

 

a1 = "samp'le s'tring"

a2 = a1.replace("'",'')

print(a2)      

 

Output:

sample string

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

For example,

1

2

3

4

5

6

 

import re

a1 = "samp'le s'tring"

a2 = re.sub("'",'',a1)

print(a2)      

 

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,

1

2

3

4

5

 

a1 = "'sample string'"

a2 = a1.strip("\'")

print(a2)      

 

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 re.sub()3 Function to Remove Quotation from CSV in Python.

A CSV is a simple comma-separated file. We can read CSV files using the re.sub()4 function in Python. The re.sub()3 file can parse the contents of the CSV file into rows.

There is a re.sub()6 parameter in the re.sub()3 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.

1

2

3

4

 

"name","age"

"java2blog","15"    

 

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

See the code below.

1

2

3

4

5

6

7

8

 

import csv

with open('csvsample.csv', 'r') as f:

    s= csv.reader(f, delimiter=',', quotechar = '"')

    for i in s:

        for j in i:

            print(j)    

 

Output:

name
age
java2blog
15

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

Using the re.sub()8 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 re.sub()9 function to read the file and store its contents in the DataFrame. The quotation marks will be removed this way.

For example,

1

2

3

4

5

 

import pandas as pd

df = pd.read_csv('csvsample.csv')

print(df)

 

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 for0 Function to Remove Quotation Marks from Data Frame in Python

The for0 function can replace characters from a for2 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.

1

2

3

4

5

6

 

import pandas as pd

df = pd.DataFrame({'A': ['"hello"', 'h"i', 'he"y"']})

df['A'] = df['A'].str.replace('"',"")

print(df)    

 

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.

How do you print a raw string in Python without quotes?

If you want to remove the enclosing quotes from a string before printing it, you can call the string. strip() method and pass the single and double quotes characters to be stripped from the beginning and end of the string object on which it is called. For example, the expression '"hello world"'.

How do you pass a string without quotes in Python?

To print a string list without quotes, use the expression '[' + ', '. join(lst) + ']' to create a single string representation of the list without the quotes around the individual strings.

How do you remove quotes from a string in Python?

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.

How do I print a raw string in Python?

You can create a raw string in Python by prefixing a string literal with r or R . Python raw string treats the backslash character (\) as a literal character.