Editor settings

General settings

Save settings

Learn to program with Python

String Escaping with Quotation Marks #

In Python, strings are defined using either single quotes ('...') or double quotes ("..."). If a string contains a quotation mark of the same type that is being used to define the string, the program will get confused and will assume that the string has ended. This is where the concept of "escaping" comes in.

To include a quotation mark of the same type that is being used to define the string, you can escape it by adding a backslash \ before the quotation mark.

For example, this is okay because the quotation marks that are being used are of a different type:

print('He said, "Hello!"')

But if you wanted to use double quotes to define the string, but the string also contains double quotes, you would escape the quotes like this:

print("He said, \"Hello!\"")

Exercise #

  • Fix the value of sentence_1 to be I'm going to the store.
  • Fix the value of sentence_2 to be She said, "I'll be there soon."
Output will be displayed here