简体   繁体   中英

Escape Sequences Exercise in Python

I'm -extremely- new to programming, and so I decided to start with Python.

So far I have been learning slowly from http://cscircles.cemc.uwaterloo.ca and so far I am understanding most of it, however I am stuck on lesson 3's exercise regarding escape sequences.

The exercise says to write a program that prints the following: A double-quote's escaped using a backslash, eg \"

I have used this as my answer so far: print("A double-quote's escaped using a backslash, eg \") but I can't seem to get the quotes to show after the backslash. I am hoping someone will hopefully explain this to me.

Thanks ahead of time,

-Fear

Why do we need escaping of strings?

It is because we want to use some special characters in our string. What do you mean by special characters? Special characters are something that would be used in constructing the string itself.

eg. " or '

Now if you have this string, "I like the symbol " !!!"

Python sees the first " and the next " and thinks the string ended and it gives an error there.

But adding a backslash makes the possible because the backslash tells python that the next character is to be read as part of string and not as a special character. In your case, you are telling python to not use the closing " as a special character, so it's bound to give an error.

Your solution will be print("A double-quote's escaped using a backslash, eg \\"")

This is because the second last " is evaluated as part of string and last " is a special character.

Additionally python offers the feature that you can make this work using,

print('A double-quote's escaped not using a backslash, eg \\"')

because in this case, " is no longer a special character because ' is taking care of it. Even the reverse case works,

print("A single-quote's escaped not using a backslash, eg '")

Print("\\\" \" ")
Print(" \" \' ")

You have two options here:

print ("My \"super awesome\" escaped string")

(--> My "super awesome" escaped string )

This fulfills the requirement, but just using a single quote is probably more pythonic:

print ('My "super awesome" single-quoted string')

(--> My "super awesome" single-quoted string )

You have two way to do that:

print 'print a double-quote: " '

or

print "print a double-quote: \" "

In most language a string start by one of this symbol: ' or " and finish by the same one.
So if you start your string with " you have to finish it with " (same goes for ')
But if you want to print the symbol itself that you use to star/end your string, you need to escape it before with a \\ to tell to the computer this is not the end of my string keep going.

This won't work:

print "print a double-quote: " "

The computer will think that the string will finish at the second " not the third one, that's what you need to add a \\ before then the computer will know that's not the end of the string but the character itself.

print("A double-quote's escaped using a backslash, eg \"") try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM