Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Mauro Y.
Mauro Y.
6,520 Points

Syntax Error

It's have a way to attribute this string:

[<input alt="Atualizada em 23/02/2017" name="jpybrl" title="TTS-JPY/BRL" type="hidden" value="38.3300"/>]

to a variable as this:

str = "[<input alt="Atualizada em 23/02/2017" name="jpybrl" title="TTS-JPY/BRL" type="hidden" value="38.3300"/>]"

Without getting "SyntaxError: invalid syntax"?

2 Answers

The reason your code is causing a Syntax Error is because you have double quotes inside your string while Python thinks that is the end of your string.

So, The double quotes inside the string to Python is actually the end of the string. As soon as Python sees a double quote, it assumes it's the end of the string, but really you don't want that.

There are two ways to fix this:
  • First, you can use a backslash \ in front of every double quote in the string to tell Python to literally use the character " in the string, not to see the double quote as the end of the string. This is called escaping.
  • Another way to solve this is using single quotes to surround the string. This way when Python sees the double quote it won't mark it as the end of the string because you used single quotes to open/close the string.

If for some reason you have both double and single quotes in your string, you can either use escaping OR you can use triple double/single quotes around the string.

I hope this helps :grin:

Happy coding! :tada:

:dizzy: ~Alex :dizzy:

Mauro Y.
Mauro Y.
6,520 Points

Understood. Worked with single quotes! Thank you very much!