Skip to main content

Unit 1 · What is Python?! · Lesson 8 of 57

The skies (and quotes) are the limit!!

The skies (and quotes) are the limit!

So wait, we can put anything in a string? Yeah, no. Python has limitations.

For example:

"The CyberAcad website clearly says "$0 cost to students" on it."

What's the issue? It's the quotes.

Remember how we said Python treats everything as plain text until it hits the closing quote? Well, that applies here too.

Python reads the string, sees the quote before $0, and assumes that's the end of the string. We can tell otherwise, but Python doesn't rely on common sense or context. It just reads it as it is.

So how do we put quotes in a string? Introducing: the backslash!

This is a valid string:

"The CyberAcad website clearly says \"$0 cost to students\" on it."

Whenever Python sees a backslash in a string, it looks at the next character and interprets it differently. In this case, it reads the quote after the backslash as part of the string instead of the end of it. The backslash itself won't show up in the final string!

This is called escaping.

You can also just wrap your string in the opposite type of quote if you really want to, but escaping is more fun! For example, this works too:

'The CyberAcad website clearly says "$0 cost to students" on it.'

Now the next question is, how do we type a regular backslash? By typing two of them, silly!

"There is a forward slash, /, and a backslash, \\."

There are many more things you can do with escaping, like other escape sequences, but we'll get into that later.

Do this lesson for real

With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.