在Python中添加单三引号变量

发布于 2025-01-13 19:48:01 字数 342 浏览 0 评论 0原文

这可能是最简单的问题,我已经尝试过三重双引号的解决方案。

但根据 Mixpanel 文档,他们传递单三引号,如下所示。

我必须在这里传递我的日期变量。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

漫漫岁月 2025-01-20 19:48:01

您可以使用 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'}}
    ]
  }})
}}'''

请注意,您需要将大括号转义为 {{}}

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

若有似无的小暗淡 2025-01-20 19:48:01

您可以使用“f”前缀字符串在另一个字符串中插入 Python 表达式。

你想要的可能是:

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. 

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. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文