Free Python course. Sign Up for tracking progress →

Python: Quotes

In this lesson, we'll learn what a string is and what role quotation marks play in code.

The definition of a string is quite simple; it's a set of characters. Let us imagine that we have these entries:

'Hello'
'Goodbye'
'G'
' '
''

Which of these are strings? In fact, all five of them are:

  • С 'Hello' and 'Goodbye' everything is obvious, we've already worked with similar constructions and called them strings
  • 'G' and ' ' — are also strings, but they only have one character each
  • '' — is an empty string, so it has zero characters

We consider anything inside quotation marks a string; even if it's just a space, a single character, or no characters at all.

Above we wrote the strings in single quotes, but this is not the only way. You can also use double quotes:

print("Dracarys!")

Now imagine you want to type the string Dragon's mother. The apostrophe before the letter s — is the same symbol as the single quote. Let's print it:

print('Dragon's mother')
# SyntaxError: invalid syntax

This program won't work. From Python's point of view, the line started with a single quote and then ended after the word dragon. Next were the characters s mother without quotation marks, so it's not a string. And then there was a one line-opening quotation mark that was never closed: `). This code contains a syntax error – you can even tell by the way the code is highlighted.

To avoid this error, we use double quotes. This version of the program will work correctly:

print("Dragon's mother")

Now the interpreter knows that the string started with a double quotation mark and must end with a double quotation mark. And the single quote inside has become the part of the string.

It works the other way too. If you want to use double quotes inside a string, you should enclose the string in single quotes. And the number of quotation marks inside the string itself doesn't matter.

Now imagine we want to create this string:

Dragon's mother said "No"

It has both single and double quotes. We need to somehow tell the interpreter that the quotation marks are one of the characters inside the string, not the beginning or the end of the string.

The escape character is used for this: \ — a backslash. If we put \ in front of a quotation mark (single or double), the interpreter will recognize the quotation mark as an ordinary character inside the string, not the beginning or the end of the string:

# We escape the quotation marks around No so that the interpreter
# can recognize them as part of the string
print("Dragon's mother said \"No\"")
# => Dragon's mother said "No"

Note that in the example above we didn't have to escape the single quote ('s) because the string itself was created with double quotes. If the string were written in single quotes, the escape character would be used before the apostrophe, not before the double quotes.

If you need to put a backlash in the string, this rule still works. Like any other special character, it must be escaped:

print("\\")
# => \

Instructions

Write a program that prints:

"Khal Drogo's favorite word is "athjahakar""

The program should display this exact phrase on the screen. Note the quotes at the beginning and the end of the phrase:

"Khal Drogo's favorite word is "athjahakar""
The exercise doesn't pass checking. What to do? 😶

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨

Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.

My code is different from the teacher's one 🤔

It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.

In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.

I've read the lessons but nothing is clear 🙄

It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.

By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.


If you got stuck and don't know what to do, you can ask a question in our community