【问题标题】:What these curly braces mean in python Django? *{}*这些花括号在 python Django 中是什么意思? *{}*
【发布时间】:2020-11-05 13:03:18
【问题描述】:

我正在做一些代码重构,我遇到了这些坏人。他们的海豚是什么? 看起来像格式化程序,但我不太确定。

                if data:
                    result = ''

                    for recipe_data in data:
                        url = recipe_data['url']
                        name = recipe_data['name']
                        rating = recipe_data['rating']
                        rating_count = recipe_data['ratingcount']
                        prep = recipe_data['prep']
                        cook = recipe_data['cook']
                        ready_in = recipe_data['ready in']
                        calories = recipe_data['calories']

                        result += """

{} {} 卡路里

谢谢。

【问题讨论】:

标签: python django


【解决方案1】:

以下是Python中花括号的用例:

1.定义字典

my_dict = { 'key': 'value' }
  1. 定义集合
my_set = {1, 2, 3, 4, 5}
  1. 格式化字符串
message = "My name is {} and I am {} years old".format(name, age)

【讨论】:

    【解决方案2】:

    它们可用于将字典定义为字符串格式

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 2012-03-22
      • 1970-01-01
      相关资源
      最近更新 更多