String Equivalent Using Variables - Python 3.x

1

What would be the equivalent of the string:

values = """
  {
   "exchange_code": "PLNX",
   "exchange_market": "BTC/USDT"
  }
"""

What the result is:

  \ n "\ n" \ n "\ n" \ n "\ n" \ n "

Entering "PLNX" and "BTC / USDT" as variables, I'm trying some variations like the one below but I can not replicate the above result:

def equi_string(exchange,market):
  values = """
    {
     "exchange_code": """+exchange+""",
     "exchange_market": """+market+"""
    }
  """
  return values
  

'\ n {\ n "exchange_code": PLNX, \ n "exchange_market": BTC / USDT \ n} \ n'

This is for example "missing" in PLNX and BTC / USDT

How do I make the string return exactly the first example?

    
asked by anonymous 23.03.2017 / 21:40

1 answer

0

If you just want to create the string in this format, try to use the "\" character to escape the quotes:

def equi_string(exchange,market):
  values = """
    {
     "exchange_code": \""""+exchange+"""\",
     "exchange_market": \""""+market+"""\"
    }
  """
  return values

Another option is to use the format, you just need to escape the "{" and "}" keys:

def equi_string(exchange,market):
   values = """
     {{
      "exchange_code": "{}",
      "exchange_market": "{}"
     }}
   """
   return values.format(exchange,market)

But if you need to create more objects in that same format more complex than this, I recommend using JSON.

    
31.03.2017 / 03:40