What other characters need to be escaped in a string?

Question

What other characters need to be escaped in a string?

Answer

You’ll typically discover these as you code and realize your output looks odd, but some common ones are:
\\, which will display the backslash
\t, which displays a tab-length spacing
\n, which displays a new line
and \' and \", which display the single and double quotes

7 Likes

Thank you for your post!

1 Like

What is the purpose of “backslash” while we can just do:
print “And whatever the code or the sentence is”?
Note: I am just a beginner!
Thanks.

1 Like

Escapement is how we tell the parser to treat the value following it as a printable character.

print ("\"\"")

will output, "" By escaping the character it is no longer interpreted as a string delimiter, but a string character.

From the early days, (not sure of the origins, exactly) we have what used to be called, escape sequences. They were principally used to perform operations on the output stream, either terminal or line printer.

\n    =>  newline
\t    =>  tab

among many others which can be researched. Not important, here. It’s enough to focus in on the printable character aspect.

What this means is the \ is not a printable character unless we escape it.

print ("\\")

will output, \. This is an example of what we call special characters and if we wish to use them in certain sequences as printable, then they need escapement.

Again, stick with the focus, and let this other stuff take up some of your weekend reading.

2 Likes

Thanks for the clarification! What if we have a long paragraph full of many special characters? Do we still need escapement ( \ ) for all of them?

1 Like

If we’re faced with that challenge, then it follows we will be informed as what those special characters are and their expected behavior.

In my experience, escapement has rarely been needed if we can limit our use of special characters.

We still have alternate string delimiters.

print ('""')    ""

print ("""'""'""")  '""'

print ('''"""'""'""""''')    """'""'"""
1 Like

If you just use print, it works just fine!

I used print(“don’t do that”)and it worked.