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_1to beI'm going to the store. - Fix the value of
sentence_2to beShe said, "I'll be there soon."
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules