Home > front end >  Adding variable in single triple quotes in Python
Adding variable in single triple quotes in Python

Time:03-13

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

CodePudding user response:

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 }}.

CodePudding user response:

You can use the "f" prefixed strings to be able to interpolate Python expressions in another string.

What you want is probably:

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'}}
  ]
}})
 }}'''


print(query1)

--
Note that the original ocurrences of `{}` have to be doubled so that they are escaped. 
  • Related