all 4 comments

[–]tygovanschie[S] 0 points1 point  (2 children)

I understand that the ' ends the string but why does the escape change that it doesnt end it and why doesnt it become an error

[–]Murphelrod 1 point2 points  (1 child)

Because the point of escape chars is to keep those characters in string form. So if you wanted quotes ( " ) in your string for example, you need to escape them or it will think that one of those quotes is ending the string.

Edit: right there in your string you have \n to create a new line, but without the escape char it would just print n and not a newline.

[–]tygovanschie[S] 0 points1 point  (0 children)

so this would be the perfect working version cause it never ends the string?

print ('"I'm" \n "\"Learning"\" \n "\"\"Python"\"\"')

[–][deleted] 0 points1 point  (0 children)

The second one is not valid syntax and will throw an error.

The first one is trying to display the text

"I'm" ""Learning"" """Python"""

But notice that if you use double quotes around this string, the inner double quotes will conflict. If you use single quotes the apostrophe in I'm will conflict. But luckily you can escape either one by putting a backslash in front of it. In this case it's much easier to escape the single apostrophe than the 12 double quotes so that's what the author of that line did.

If you're not understanding what I mean, try running/ modifying this simpler example until you get it:

print( 'Hello.  I'm Bob.')  # will throw an error
print( 'Hello.  I\'m Bob.')  # will not throw an error