assert pop == "berg", "Item removed from the stack was not \"berg\"."

why using \'...\' can avoid syntax error ?

Thank you :)

8 Years
Discussion Span
Last Post by eva yang

The backslash (\) is used for an "escape character". So \t is the escape character for a tab, \n is for newline, and \" or \' are escapes for a double and single quote respectively.

Let's say we have a string: 'Hi my name is Charlie' . The single quotes are what tells the interpreter that every character in between is part of the string. Now if we wanted to use a contraction inside that string and change it to: 'Hi my name's Charlie' , you can see that the string (ie, what's inside the quotes) is actually only 'Hi my name' and the rest is attempted to be interpreted to Python code.

To solve this there are two methods:

1) Change the type of quotation marks that are used on the string, so we would instead have: "Hi my name's Charlie" 2) Escape the "inner" quotation so that it is interpreted as the character instead of the syntax marker: 'Hi my name\'s Charlie' HTH

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.