以Python方式使用f-String进行字符串格式化



我是python和学习字符串格式的新手。我用f-string格式化这个字符串,我用%s格式化,但我想用f-string来格式化。

query = f'''
summary {
operation(
input: {
operation_type: accounts,
user_id: {user_id},
user_secret: {user_secret},
code: {user_id}
}
) {
user_accounts,
user_account_type
}
}'''

由于{}用于转义变量名,因此需要将它们加倍以表示实际的大括号:

query = f'''
summary {{
operation(
input: {{
operation_type: accounts,
user_id: {user_id},
user_secret: {user_secret},
code: {user_id}
}}
) {{
user_accounts,
user_account_type
}}
}}'''

最新更新