pythonpython-3.xmixpanel

Adding variable in single triple quotes in Python


This might be the simplest question, I have tried the solution for triple double-quotes.

But as per Mixpanel documentation, they are passing single triple quotes like below.

I have to pass my date variable here.

fromdate = '2022-03-12'
todate = '2022-03-12'

query1 = '''function main() {
return Events({
  from_date: fromdate,
  to_date:   todate,
  event_selectors:[
  {'event':'Event name'}
  ]
})
 }'''


print(query1)

3


Solution

  • You can use f-strings

    fromdate = '2022-03-12'
    todate = '2022-03-12'
    
    query1 = f'''function main() {{
      return Events({{
        from_date: {fromdate},
        to_date:   {todate},
        event_selectors:[
          {{'event':'Event name'}}
        ]
      }})
    }}'''
    

    Do notice that you need to escape the curly braces as {{ and }}.