Search code examples
pythonstringstring.formatquotation-marks

Is there a way to avoid double quotation in formatting strings which include quotations inside them in Python


It is not supposed to be a hard problem, but I've worked on it for almost a day!

I want to create a query which has to be in this format: 'lat':'###','long':'###' where ###s represent latitude and longitude.

I am using the following code to generate the queries:

coordinateslist=[]
for i in range(len(lat)):
     coordinateslist.append("'lat':'{}','long':'-{}'".format(lat[i],lon[i]))
coordinateslist

However the result would be some thing similar to this which has "" at the beginning and end of it: "'lat':'40.66','long':'-73.93'"

Ridiculously enough it's impossible to remove the " with either .replace or .strip! and wrapping the terms around repr doesn't solve the issue. Do you know how I can get rid of those double quotation marks?

P.S. I know that when I print the command the "will not be shown but when i use each element of the array in my query, a " will appear at the end of the query which stops it from working. directly writing the line like this:

query_params = {'lat':'43.57','long':'-116.56'}

works perfectly fine.

but using either of the codes below will lead to an error.

aa=print(coordinateslist[0])
bb=coordinateslist[0]

query_params = {aa}
query_params = {bb}
query_params = aa
query_params = bb

Solution

  • Try using a dictionary instead, if you don't want to see the " from string representation:

    coordinateslist.append({
        "lat": lat[i],
        "long": "-{}".format(lon[i])
    })