FAQ: String Methods - .format()


#1

This community-built FAQ covers the “.format()” 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()

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

In the practice for .format() the backslashes are not included, yet I was supposed to know that it was needed? Why was it not shown in the example code?


#3
"The poem \"{}\" is written by {}."

The topic of escape characters must have come up in early lessons on strings.

Above, the quotes inside the string are escaped so they are ignored by the string parser and render as printed characters.

The output will look like,

The poem "I Hear America Singing" is written by Walt Whitman.

We can also write the code so the string is in single quotes. This way no excapement is required.

'The poem "{}" is written by {}.'

#4

Why are the arguments flipped around when it comes to the .format() code? According to codecademy, the top is incorrect, and the bottom is correct. Why, though?

Originally I wrote it this way:

def poem_title_card(title, poet):
  return "The poem \"{}\" is written by {}".format(title, poet)

I’m very confused. This is the code that codecademy gave as the solution:

def poem_title_card(poet, title):
  poem_desc = "The poem \"{}\" is written by {}".format(title, poet)
  return poem_desc

codecademy didn’t flip the arguments around in the example, so I don’t understand why this is happening now.


#5

They are flipped around because of the order in which they are interpolated in the string. TITLE, first, then, POET. It doesn’t matter what order they are given in the parameter so long as the arguments in the call expression match the correct variable.

If the parameters are written,

(title, poet)

then arguments must be,

("I Hear America Singing", "Walt Whitman")

else if written as such,

(poet, title)

then the arguments must be,

("Walt Whitman", "I Hear America Singing")

Either way won’t matter to the output string. The format arguments are ordered according to how they present.

Now in a case where we wish to have both lists the same, say,

(poet, title)

in the parameters, and,

(poet, title)

in the format arguments, we can use numbers in the placeholder to identify which argument goes where…

'The poem "{2}" is written by {1}.'.format(poet, title)

#6

Instructions say which order the arguments will be provided in to the function


#7

Thanks, that’s very useful to know, especially that last bit about placeholders.

I do understand why the format arguments have to be in a certain order (“according to how they present”), but I’m not sure why the arguments in def poem_title_card(title, poet): need to be flipped. Why can it not be def poem_title_card(poem,title)?


#8

I can see that, too, but is it that way just because the instructions arbitrarily say so?

I don’t see what makes this example so different from the Santana example, that the order the arguments are provided to the function needs to be vice versa.


#9

Argument order is always significant, that is what says which value is which
It’s the same thing as when you send arguments to str’s format method, the order is what says which is which


#10

So then why does the Santana example need the arguments provided in the opposite order?


#11

Opposite to what?

If you mean that there ought to be consistency between the order of title-author … sure? But python certainly doesn’t care about that


#12

in the original argument, it asks for “song, artist,” and then, after .format(, “song, artist.”

Yet in the example I just provided, which is very similar to the Santana one, I need to flip around the arguments. I don’t need to do that with the Santana example.

furthter response:

Then why wouldn’t Python accept this code as correct?:

def poem_title_card(title, poet):
  poem_desc = "The poem \"{}\" is written by {}".format(title, poet)
  return poem_desc

WHAT IS WRONG WITH THAT CODE?? Python says that it’s wrong.

codecademy is not letting me write anymore messages :confused:


#13

Like you point out, codecademy’s designs of the function signatures are a bit inconsistent but it doesn’t have anything to do with python does it?

They say which order the arguments are provided, and yeah they don’t quite match but I kind of fail to care, just write the functions to the provided specs

Good job hitting that limit. I raised it. I think.


#14

yes but I’m new to learning Python, so when these errors occur, it makes me think that I did something wrong. So I could have written what I originally wrote, and it should have been okay?

And thanks for raising the limit. I’m not trying to be a nuisance.


#15

Nah that limit only exists to prevent sudden large quantities of spam.

The order doesn’t match in the specification, but… The easiest way for me to express it is that I’m not bothered by it, and I’m easily bothered by issues is exercises.
You ought to be reading the instructions and matching your code to them… It’s not wrong in some way, just different.
Both of those instructions say which order the values will be fed to your function.

If you were writing a library with many exported function where signatures were similar but had very different ways to use them, like different orders then, yeah that’d be poor design. Here though? Meh.


#16

Point taken, I will follow the instructions as given to me from now on. But just to be clear, the error message at the bottom doesn’t make sense, does it?:

my format arguments are in the correct order, yet look at what it says that my code is returning.

According to that, Walt Whitman is the “title” and “I hear America Singing” is the poet.


#17

No your format arguments are in the wrong order, because the title should come first and then the poet, but you’ve done it the other way around because the first argument provided to the function is the poet

(if you rename your parameters to arg1 and arg2 that makes more sense)

The order they appear in the string shouldn’t be considered related to the order of the parameters

At a glance anyway (not going to bother comparing exactly), the error message seems to match the instructions


#18

Thanks, that does make sense.

Wow, it just hit me like a ton of bricks. Ugh, I’m sorry.