如何在Python中的多行字符串中包含变量?

问题描述 投票:0回答:1

例如,我有以下内容:

first_count = 5000
query = '''
{
   students(first: first_count) {
      id
      firstName
      college {
         id
         name
         location
         rating
      }
   }
}
'''

在查询的第二行中,first_count 应该是一个变量,但不幸的是我很难使其动态化。我想做一个 f 字符串,但我丢失了多行字符串并且还使用了转义字符。有什么方法可以在此字符串中插入变量以使其动态,同时保持多行格式?

python python-3.x string format
1个回答
0
投票

是的,你可以这样解决:

first_count = 5000
query = f'''
{{
  students(first: {first_count}){{
      id
      firstName
      college {{
         id
         name
         location
         rating
     }}
   }}
}}
'''

print(query)
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.