Escape Coded Strings

Some characters cannot be included literally in string constants. Instead, they should be represented with escape sequences, which are character sequences beginning with a backslash (\).

One use of an escape sequence is to include a double-quote character in a string constant. Because a plain double quote ends the string, you must use (\") to represent an actual double-quote character as a part of the string.

For example:

print "He said \"hi!\" to her."

He said "hi!" to her.

The backslash character itself is another character that cannot be included normally; you must write (\\) to put one backslash in the string. Thus, the string whose contents are the two characters ("\) must be written (\"\\).

Backslash can also be used to insert unprintable characters such as TAB or newline.

Escape sequences in strings are interpreted according to rules similar to those used by Standard C or Java.

The recognized escape sequences are:

Escape Sequence Meaning
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\ooo Character with octal value ooo
\xhh Character with hex value hh
\uxxxx Character with 16-bit hex value xxxx (Unicode)

Become a Patron