Kumar
Kumar

Reputation: 809

Convert single-quoted string to double-quoted 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.

Upvotes: 41

Views: 125035

Answers (8)

Olamide Samuel
Olamide Samuel

Reputation: 36

If you're here because you have have a dict object that you converted to a string using str() and would like the dictionary back, doing json.loads(str_stuffs.replace("'", '"')) should help. So for example,

import json

stuffs = {"stuff1": "value", "stuff2": "value", "stuff3": "value"}
str_stuffs = str(stuffs)

original_stuffs = json.loads(str_stuffs.replace("'", '"'))

Hope this helps!

Upvotes: 0

Mujtaba Ali
Mujtaba Ali

Reputation: 1

If you're talking about converting quotes inside a string, One thing you could do is replace single quotes with double quotes in the resulting string and use that. Something like this:

def toDouble(stmt):
    return stmt.replace("'",'"')

Upvotes: 0

Bruno Castro
Bruno Castro

Reputation: 29

Actually, none of the answers above as far as I know answers the question, the question how to convert a single quoted string to a double quoted one, regardless if for python is interchangeable one can be using Python to autogenerate code where is not.

One example can be trying to generate a SQL statement where which quotes are used can be very important, and furthermore a simple replace between double quote and single quote may not be so simple (i.e., you may have double quotes enclosed in single quotes).

print('INSERT INTO xx.xx VALUES' + str(tuple(['a',"b'c",'dfg'])) +';')

Which returns:

INSERT INTO xx.xx VALUES('a', "b'c", 'dfg');

At the moment I do not have a clear answer for this particular question but I thought worth pointing out in case someone knows. (Will come back if I figure it out though)

Upvotes: 1

jsbueno
jsbueno

Reputation: 110696

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('"', "'")

Upvotes: 27

MadsVJ
MadsVJ

Reputation: 708

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"

Upvotes: 24

KocT9H
KocT9H

Reputation: 349

In my case I needed to print list in json format. This worked for me:

f'''"inputs" : {str(vec).replace("'", '"')},\n'''

Output:

"inputs" : ["Input_Vector0_0_0", "Input_Vector0_0_1"],

Before without replace:

f'"inputs" : {vec},\n'

"inputs" : ['Input_Vector0_0_0', 'Input_Vector0_0_1'],

Upvotes: 6

Ben Hayden
Ben Hayden

Reputation: 1379

In Python, there is no difference between strings that are single or double quoted, so I don't know why you would want to do this. However, if you actually mean single quote characters inside a string, then to replace them with double quotes, you would do this: mystring.replace('\'', '"')

Upvotes: 4

Brian R. Bondy
Brian R. Bondy

Reputation: 347566

The difference is only on input. They are the same.

s = "hi"
t = 'hi'
s == t

True

You can even do:

"hi" == 'hi'

True

Providing both methods is useful because you can for example have your string contain either ' or " directly without escaping.

Upvotes: 5

Related Questions