Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert single-quoted string to double-quoted string

Tags:

python

string

I want to check whether the given string is single- or double-quoted. If it is single quote I want to convert it to be double quote, else it has to be same double quote.

like image 451
Kumar Avatar asked Dec 15 '09 12:12

Kumar


People also ask

How do I convert a string to a double quote in Python?

In a string enclosed in double quotes " , single quotes ' can be used as is, but double quotes " must be escaped with a backslash and written as \" . There is no problem if you write \' for single quotes ' .

How do you make a double quoted string?

If you need to use the double quote inside the string, you can use the backslash character. Notice how the backslash in the second line is used to escape the double quote characters. And the single quote can be used without a backslash.

How do you replace a single quote in a string in Python?

Remove Quotes From String in Python Using the replace() Method. This method takes 2 arguments, which could be named as old and new. We can call the replace() , with '""' as the old string and "" (empty string) as the new string, to remove all quotes.

What is the difference between single quoted and double quoted strings?

There is no difference between "single quoted" and "double quoted" strigns in Python: both are parsed internally to string objects.

How to convert string to double quote text in R?

dQuote () function in R Language is used to convert the given string or character vector into double quote text.

How do you escape a double quote in a string literal?

Since, for defining string literal, we are using double-quote string, that’s why we need to escape the double-quotes (“”) using a backslash (\) within the string. The engineer is quoted in a single-quoted string.

What are the alternatives of single quote or double quote?

Example 2: Using a back-tic or a diacritic sign (`) is the alternative of using single quote or double quote. In this you don’t need to escape any single quote or double quote. So, it is an error free choice. Again the string enclosed in back-tic would be strictly and exactly equal to the strings enclosed in single or double quotes.


2 Answers

There is no difference between "single quoted" and "double quoted" strings in Python: both are parsed internally to string objects.

I mean:

a = "European Swallow" b = 'African Swallow' 

Are internally string objects.

However you might mean to add an extra quote inside an string object, so that the content itself show up quoted when printed/exported?

c = "'Unladen Swallow'" 

If you have a mix of quotes inside a string like:

a = """ Merry "Christmas"! Happy 'new year'! """ 

Then you can use the "replace" method to convert then all into one type:

a = a.replace('"', "'")  

If you happen to have nested strings, then replace first the existing quotes to escaped quotes, and later the otuer quotes:

a = """This is an example: "containing 'nested' strings" """ a = a.replace("'", "\\\'") a = a.replace('"', "'") 
like image 170
jsbueno Avatar answered Oct 02 '22 16:10

jsbueno


Sounds like you are working with JSON. I would just make sure it is always a double quoted like this:

doubleQString = "{0}".format('my normal string') with open('sampledict.json','w') as f:     json.dump(doubleQString ,f) 

Notice I'm using dump, not dumps.

Sampledict.json will look like this:

"my normal string" 
like image 32
MadsVJ Avatar answered Oct 02 '22 18:10

MadsVJ