Escape Sequence (General Concept)

Introduction

An escape sequence is a combination of characters that has a special meaning.

Escape Sequences in General

  • Some characters imply a special function rather than their “normal” meaning. For example, ' often implies the start or the end of a string.
  • But what if you actually mean ' rather than a string? In that case you can escape its default meaning by prefixing it, often with a backslash (as in \').
  • Reversely, \n usually implies a new line indicator. In this case, the backslash escapes the default meaning of n (which is simply n).
  • Escape sequences thus work in two ways: they may apply a special function to a character that doesn’t have one by default as in the case of (\n) or they may disable a special function from a character that has one by default (\').

Escape Sequences in SPSS

  • In SPSS, quotes can be escaped by doubling them. This may be necessary when value labels or variable labels contain quotes as in ADD VALUE LABELS v1 1 'Don''t know'..
  • If the second quote hadn’t been doubled, it would have indicated the end of the value label immediately after “Don”.
  • An alternative here is to use double quotes around the value label and single quotes within it as in ADD VALUE LABELS v1 1 "Don't know"..

Escaping in Python

  • In Python, escape sequences are indicated by a backslash (\).
  • The most important one may be \n which indicates a new line. Like so, multiple logical lines can be stacked into a single physical line.
  • Another escape sequence worth mentioning is \' for a single quote within a string.
  • A backslash also escapes itself so if it’s really a backslash you want, double it as in \\.
  • For a demonstration of these point, run the syntax below.

Python Escape Sequence Examples

begin program.
print ‘line 1\nline 2\nline 3’
print ‘This is a backslash: \\’
print ‘Don\’t worry be happy!’
end program.

Post Author: Zahid Farid