
Ultimate access to all questions.
Question 24
A data engineer needs to dynamically create a table name string using three Python variables: region, store, and year. An example of a table name is below when region = "nyc", store = "100", and year = "2021":
nyc100_sales_2021
Which of the following commands should the data engineer use to construct the table name in Python?
Explanation:
To construct the table name nyc100_sales_2021 using Python variables region, store, and year, we need to use Python's f-string formatting correctly.
Let's analyze each option:
Option A: " {region}+{store}+_sales_{year}" - This is a regular string, not an f-string, so the variables won't be interpolated. It would literally output " {region}+{store}+_sales_{year}".
Option B: f"{region}+{store}+_sales_{year}" - This uses f-string formatting but includes + characters between variables, which would result in nyc+100+_sales_2021 instead of the desired nyc100_sales_2021.
Option C: " {region}{store}_sales_{year}" - This is a regular string without f-string prefix, so variables won't be interpolated.
Option D: f"{region}{store}_sales_{year}" - CORRECT: This uses f-string formatting correctly without any extra characters between variables. When region = "nyc", store = "100", and year = "2021", it will produce exactly nyc100_sales_2021.
Option E: {region}+{store}+_sales_"+{year} - This is invalid Python syntax that would cause a syntax error.
Key points:
f or F{}+ are needed between variables in f-strings{region}{store}_sales_{year} without any separators between region and storeTherefore, option D is the correct choice for dynamically creating the table name string in Python.