
Ultimate access to all questions.
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?
A
" {region}+{store}+_sales_{year}"
B
f"{region}+{store}+_sales_{year}"
C
" {region}{store}_sales_{year}"
D
f"{region}{store}_sales_{year}"
E
{region}+{store}+_sales_"+{year}
Explanation:
The correct answer is D because:
f-string syntax: The f"" prefix creates an f-string (formatted string literal) in Python, which allows embedding expressions inside string literals using curly braces {}.
Proper concatenation: The desired table name format is nyc100_sales_2021, which means:
region and store should be concatenated directly without any separator: nyc100_sales_yearWhy other options are incorrect:
+ signs which would create nyc+100+_sales_2021 instead of nyc100_sales_2021Testing with example values:
region = "nyc"
store = "100"
year = "2021"
table_name = f"{region}{store}_sales_{year}"
print(table_name) # Output: nyc100_sales_2021
region = "nyc"
store = "100"
year = "2021"
table_name = f"{region}{store}_sales_{year}"
print(table_name) # Output: nyc100_sales_2021
This approach correctly uses Python's f-string formatting to dynamically create the table name string with the desired format.