FAQ: String Methods - .format() II

This community-built FAQ covers the “.format() II” exercise from the lesson “String Methods”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science

FAQs on the exercise .format() II

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Do we need to define the variables seperately?

I did exactly what I was asked in this exercise, and it takes my solution for the first part, yet when I try to print it I get NameError publishing_date not defined.

Why is that?

def poem_description(publishing_date, author, title, original_work):
  poem_desc = "The poem {title} by {author} was originally published in {original_work} in {publishing_date}.".format(publishing_date=publishing_date, author=author, title=title, original_work=original_work)
  return poem_desc

From my understanding, keyword arguments are stored as temporary variables.
Do the keyword args of the function poem_description() not conflict with the keyword args of the string format because they have different scopes?

So, I feel like something really important is missing in the lesson text here. Because all of the keywords and variable names are the same (song and song, publishing_date and publishing_date), it’s not clear for the reader if the keyword or the variable is coming first inside the format().

What is mean is, which of the following is correct?

‘This is {answer}.’.format(answer=variable)

‘This is {answer}.’.format(variable=answer)

My intuition from the other lessons is keyword = variable, but that’s just intuition.

1 Like

I will post here some example using the author tag as an example:

def poem_description(publishing_date, authorVARIABLE, title, original_work):
  poem_desc = "The poem {title} by {authorANSWER} was originally published in {original_work} in {publishing_date}.".format(publishing_date = publishing_date, authorANSWER = authorVARIABLE, title = title, original_work = original_work)
  return poem_desc

So the right one is: ‘This is {answer}.’.format(answer=variable).

I hope this could still be helpful for someone.

3 Likes

I could only finish the exercise using the “view solution” button. I found same problem as you. I really think that probably it’s some kind of bug but I don’t have a clue about it.

1 Like

Just to make a note here. The answer they give us is the same program that you posted in our question.

Hello @fellrock! I believe that Codecademy wants you to return a string literal, not a variable containing a string.

Is the ability to jumble the order of the arguments in this format mutually exclusive between the string and calling the function?

For example:

my_beard_description = poem_description(“1974”, “Shel Silverstein”, “My Beard”, “Where the Sidewalk Ends”) the order of the parameters as described in the original function will return a correct, response, however if you enter the parameters while calling it as they appear in the exercise:

author = “Shel Silverstein”
title = “My Beard”
original_work = “Where the Sidewalk Ends”
publishing_date = “1974”

the value returned is “not as expected”.

3 Likes

I am wondering why we are using .format(), instead of f-strings? Using f-strings would in my opinion be much easier.

This lesson may have been written before f-strings were introduced in 3.6; it’s still important to learn about .format() though, as you’ll likely encounter both f-strings and .format() in others’ code.

1 Like

Is it not bad that we are using keywords which are the same as the variable names? Like aren’t we just self - referencing the keywords? Isn’t that bad?

1 Like

In the “Instructions” section, shouldn’t we also print the variable my_beard_description?

1 Like