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

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

Thank you :)

Recommended Answers

All 2 Replies

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

Thanks a lot. That makes sense:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.