首页 > 解决方案 > 这些花括号在 python Django 中是什么意思?*{}*

问题描述

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

                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 += """

{} {} 卡路里

谢谢。

标签: pythondjango

解决方案


以下是 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)

推荐阅读