Petal Power Inventory

Link: https://www.codecademy.com/paths/data-science/tracks/dscp-data-manipulation-with-pandas/modules/dscp-hands-on-with-pandas/projects/pandas-inventory-proj

Question 8 asks to combine 2 columns using a lambda function:

combine_lambda = lambda row:
‘{} - {}’.format(row.product_type,
row.product_description)

Can anyone explain if this can be done using an f-string and if so, how that would look like?

If I’ve interpreted you correctly the f-string equivalent would be-

from types import SimpleNamespace # Just creating a quick object with attributes to match yours row = SimpleNamespace( product_type='Toy', product_description='Mighty elephant' ) format_method = '{} - {}'.format( row.product_type, row.product_description ) fstring_method = f'{row.product_type} - {row.product_description}' print(format_method) print(fstring_method) # Are they the same? print(format_method == fstring_method)

If you’re looking for details about f-strings try Python 3's f-Strings: An Improved String Formatting Syntax (Guide) – Real Python or have a web search as there are a few existing tutorials for them.

As an aside, if you’re naming a lambda consider just using a function. Naming it defeats the point somewhat :wink: