Skip to main content This browser is no longer supported. Show
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Escape Sequences
In this articleCharacter combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant. Escape sequences are typically used to specify actions such as carriage returns and tab movements on terminals and printers. They are also used to provide literal representations of nonprinting characters and characters that usually have special meanings, such as the double quotation mark ("). The following table lists the ANSI escape sequences and what they represent. Note that the question mark preceded by a backslash (\?) specifies a literal question mark in cases where the character sequence would be misinterpreted as a trigraph. See Trigraphs for more information. Escape Sequences
Microsoft Specific If a backslash precedes a character that does not appear in the table, the compiler handles the undefined character as the character itself. For example, END Microsoft Specific Escape sequences allow you to send nongraphic control characters to a display device. For example, the ESC character (\033) is often used as the first character of a control command for a terminal or printer. Some escape sequences are device-specific. For instance, the vertical tab and form feed escape sequences (\v and \f) do not affect screen output, but they do perform appropriate printer operations. You can also use the backslash (\) as a continuation character. When a newline character (equivalent to pressing the RETURN key) immediately follows the backslash, the compiler ignores the backslash and the newline character and treats the next line as part of the previous line. This is useful primarily for preprocessor definitions longer than a single line. For example:
See alsoC Character Constants FeedbackSubmit and view feedback for Escape Sequence is a combination of characters (usually prefixed with an escape character), that has a non-literal character interpretation. Such that, the characters sequences which are considered as an escape sequence have a meaning other than the literal characters contained therein. Most Programming languages use a backslash \ as an escape character. This character is used as an escape sequence initiator, any character (one or more) following this is interpreted as an escape sequence. If an escape sequence is designated to a Non-Printable Character or a Control Code, then the sequence is called a control character. List of Escape Sequence in Python:
The above table is applicable for Python programming language, as different languages have different control sequences and control characters so the above table may not work in your programming language of choice. Ex. Windows Command Line interpreter uses a caret ( ^ ) to escape characters, and therefore the above table won’t be applicable there. Escape Sequence Interpretation Escape sequence interpretation is done, when a backslash is encountered within a string. After the encounter of a backslash (inside a string), any following character (with the ( \ )) would be looked upon the aforementioned table. If a match is found then the sequence is omitted from the string, and its translation associated with the sequence is used. If a match is not found, then no lookup happens, and the control sequence is copied as it is. Example Python3
Output: I will go Home See you\jtommorow As seen in the above output, the first print statement produced an output where the \t got resolved into a vertical tab and is omitted in the output. On the other hand, in the second print statement, the \j persists, as no legal resolution for that sequence exists. Preventing Escape Sequence InterpretationThere are instances where we don’t want the strings to behave in this way. In those cases, we generally want to preserve the backslashes. Some of the situations in which this may be required are:
Methods of PreventionMethod 1: Consistently doubling the backslashes, also allows us to overcome such issues. In this method, we manually find every single backslash in the string and concatenate another backslash to it (at its immediate position). Generally, a tedious method, and only advised if the string size is less. Python3
Output: I love to use instead of using 4 spaces I love to use \t instead of using 4 spaces Method 2: Using r’….’ or R’…..’ construct. Commonly referred to as raw strings, which is used to preserve the escape sequences as literals. Such that it does what the previous method did but automatically (does not require human intervention). For turning a normal string into a raw string, prefix the string (before the quote) with an r or R. This is the method of choice for overcoming this escape sequence problem. Python3
Output: C:\Program Files ortonppx C:\Program Files\norton\appx Problems due to escape characters may not always result in undesirable output, but also errors. For example, the below code upon execution will produce an error. Python3
Produces the following error
The error is caused because the \U in the string leads to the next 4 characters being treated as a 32-bit Hexadecimal value which would correspond to a Unicode code point. Which leads to an error as the next character is s which are outside the base 16 range. Which character signifies the beginning of an escape sequence such as creating a tab stop?Most Programming languages use a backslash \ as an escape character. This character is used as an escape sequence initiator, any character (one or more) following this is interpreted as an escape sequence.
Which escape sequence will move the cursor to the beginning of the current line?\r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
What argument in a print statement stops the output from advancing to a new line?To print without a newline, all you have to do is add an additional argument at the end of your print statement. This argument is called end.
What does the following statement mean num1 num2 Get_num ()?num1, num2 = get_num() a. The function get_num() is expected to return a value each for num1 and num2.
|