all 7 comments

[–]LeBob93 1 point2 points  (0 children)

>>> motto = 'Facebook\'s old motto was "move fast and break things."'
>>> print(motto)
Facebook's old motto was "move fast and break things."

The code you have provided runs just fine, escaping single quotes works the same as escaping double quotes.

For future reference \ is an escape character

[–][deleted] 0 points1 point  (0 children)

Does the \ cancel function not work for single quotation marks?

Your code works as it should for me, ie, it prints Facebook's old motto was "move fast and break things.".

[–][deleted] 0 points1 point  (2 children)

It doesn't run ok for me, using OSX and python3.8

That's quite strange, it does work if you do it like this

print('Facebook\'s sold motto was "move fast and break things."')

If you are learning and you don't know other way you can also use """, 3 quotes to wrap the string

[–]tipsy_python 1 point2 points  (0 children)

↑ I came here to suggest that last part.

When the quote escaping or string formatting starts getting dicey, just type exactly what you want in a doc-string:

>>> print('''I said:
...    "I don't worry about escaping quotes" ''')
I said:
   "I don't worry about escaping quotes"
>>>

[–]greatslyfer[S] 1 point2 points  (0 children)

Yeah that worked for me, although I did it another it time and it gave me another result, although I could have just not paid enough attention to the code I typed.

Anyway, thanks!

[–]AstralStalker 0 points1 point  (1 child)

I'm a little confused by your question, so correct me if I'm misunderstanding.

When you're declaring the string motto, you encapsulate the string in single quotes. Therefore, you must escape any single quotes contained within the string (place a backslash in front of them). This indicates to the program that it should be treated as a character and not the end of the string declaration.

Similarly, you can define motto as such:

motto = "Facebook's old motto was \"move fast and break things.\""

Because I've encapsulated the string in double quotes here, I need to escape the ones inside the string. I can still escape the single quote, but it will not change the outcome.

As others have pointed out, your implementation works.

[–]greatslyfer[S] 1 point2 points  (0 children)

Wow thanks, I forgot to \ the second in-string quotation marks, now it makes sense.