What are the backslashes in the string?

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?

"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 {}.'

Is there any way in which doing the method provided in this exercise:

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

is better than:

'The poem "%s" is written by %s.' % (title, poet)

The second method seems simpler to me. Am I missing something? Is there a scenario where .format() is better?

1 Like

The second example is legacy code inherited from C and written into early versions of Python, while the first was a late addition to version 2.7.x, i believe. It is far superior to the its predecessor in many ways.

This page compares the two…


Study the examples and in the end, ask yourself, which is more powerful and robust?


When the opening/closing quotes are single, we don’t need to escape double quotes in the string.

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

Im getting fed up with how the lessons are assignments do match. I still dont understand why the escapes was needed here.


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

Can someone tell me why this code above not working since that was what the lesson taught me

Because all the quotes used are the same, we need to escape the ones that will be printed…

"The poem \“{}\” is written by \“{}\”. "

Honestly im stuck although i see the solution to the lesson. I do not understand the solution.

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

The above is the solution given;

  1. title and poet positions (1st,2nd) are interchanged. yet code still works
  2. I do not see any output.
  3. The backslashes making everthing look like rocket science as compared to the intro in the lesson.

I will appreciate if someone can explain line by line for me to understand the code.

This is hardly rocket science, and it does get easier once the concepts take on more meaning. Escapement treats characters as printable.

Can you help me understand

What do you not understand?

After 1 hour of staring at the codes and using a print function. I understand it but if you can explain to me the use of the \ strokes to be better i will appreciate. especially why its bein used.

Thank you very much

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

No escape needed since the quotes are unique in the string, and different from the opening and closing quotes.

It’s when the opening and closing quotes are the same as those in the string that we need to escape the ones we want to print.


Oh okay well understood. I think i didnt read the instruction very well.

Thanks for the explanation and your patience for me to grasp the concept… I appreciate.

1 Like

instead of using backslashes, can I use “”"{}""" three quotation marks.
def poem_title_card(poet,title):
return “The poem “{}” is written by {}.”.format(title, poet)

1 Like

Yes, they are valid quote marks.

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

Or single quotes…

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

This is what I wrote and the output of the print statement is exactly as the solution required. But I get an error from the exercise. Did I miss something? or wrong?

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

print(poem_title_card("“I Hear America Singing”", “Walt Whitman”))

This is what is request as a solution:
It should return the string

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

Thank you for the help

AFAIK, Python only has '...', "...", '''...''' and """...""", but no ""..."" or ''...''.

1 Like

Exactly. If the solution demonstrates a backslash, then a hint should indicate that.

Here is my logic, thus far:

our defined function, def poem_title_card, which takes two inputs, the first being title and the second as poet is written as:

def poem_title_card (title, poet):

Using the .format( ) string method in Python, the returned string is written as:

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

Therefore, our final result is:

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

How are backslashes justified if the tutorial does not indicate backslashes.

The grammar for printing the name of a poem in a sentence is to put it in quotes. If we wish to have double quotes, and insist that our sentence is not quoted with apostrophes so they can be used in the sentence, then we have a need for escapement.

"the poem \"{}\" is written by {}"