【问题标题】:String Formatting using f-strings in Pythonic way [duplicate]以Pythonic方式使用f字符串进行字符串格式化[重复]
【发布时间】:2021-07-08 08:57:19
【问题描述】:

我是 python 和学习字符串格式的新手。我正在使用 f-strings 格式化此字符串,我使用 %s 执行此操作,但我想使用 f-strings 执行此操作。

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

【问题讨论】:

    标签: python python-3.x string string-formatting


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      相关资源
      最近更新 更多