How do you replace double quotes in a string in python?

I have a string -

l = '{"a": "1", "b": "2"}'

I want to convert this string to -

'{\"a\": \"1\", \"b\": \"2\"}'

For this I am trying to replace " with \"

Here's what I have tried -

l.replace('\"', '\"')
'{"a": "1", "b": "2"}' 

l.replace('\"', '\\"')
'{\\"a\\": \\"1\\", \\"b\\": \\"2\\"}'

How do I convert {\"a\": \"1\", \"b\": \"2\"}?

asked Feb 1, 2017 at 1:42

6

Try this:

print l.replace('"','\\"')

'\"' doesn't mean anything special to Python, so you needn't to add \ before ",if you run

print l.replace('\"', '\\"'),you will get a single backslash too.

Actually what you are seeing is the representation of the string, it's added by repr() method.Python represents backslashes in strings as \\ because the backslash is an Escape Character .

If you print it, you will get single backslash.

You can see more information from String and Bytes literals.

answered Feb 1, 2017 at 1:50

McGradyMcGrady

10.1k13 gold badges46 silver badges64 bronze badges

1

You can try this also

print l.replace('"',r'\"')

or

print l.replace('"','\\"')

answered Feb 1, 2017 at 1:55

How do you replace double quotes in a string in python?

AvinashAvinash

2,0032 gold badges25 silver badges40 bronze badges

Introduction

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.

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:

new_str = example_str.replace('"', '')
print(new_str) # This is a string without quotes

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 re module:

import re

After that, we can use the re.sub() method to substitute all occurrences of the quotation mark with the empty string:

new_str = re.sub('"', '', example_str)
print(new_str) # This is a string without quotes

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

str.join()

The str.join() 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 join() 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:

new_str = ''.join(c for c in example_str if c not in '"')
print(new_str) # This is a string without quotes

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 str.strip() method is intended to do just that. Therefore, we'll discuss the str.strip() 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 str.strip() 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!

new_str = example_str.strip('"')
print(new_str) # This is a string" without quotes

Note: No matter how many leading and/or leading quotation marks are in the string, the str.strip() will remove all of them.

Besides the usual str.strip() method, there are also two of its derivatives - str.lstrip() and str.rstrip(). The first one removes all leading quotes, and the second removes all trailing quotes:

new_str = example_str.lstrip('"')
print(new_str) # This is a string" without quotes"

new_str = example_str.rstrip('"')
print(new_str) # "This is a string" without 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(), str.join(), and re.sub(). Afterward, we've taken a look at the str.strip() method, as well as its derivatives str.lstrip() and str.rstrip().

How do you replace double quotes in Python?

Replace Single Quotes With Double Quotes in Python.
Method 1: Using the str.replace() function..
Method 2: Using the sub() function in re package..
Method 3: Using translate() function in Python..
Method 4: Using the JSON module..

How do you remove double quotes from a string in Python?

Use str. strip(chars) on str with the quote character '"' as chars to remove quotes from the ends of the string.

How do you remove double quotes from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you replace a quote in a list in Python?

Remove double quotes from a List of strings in Python #.
Use a list comprehension to iterate over the list..
Use the str. replace() method to remove the double quotes from each string..
The items in the new list won't contain double quotes..