What Is the Backslash Character (\\)

What is the backslash character (\\)?

\ is used as for escape sequence in many programming languages, including Java.

If you want to

  • go to next line then use \n or \r,
  • for tab use \t
  • likewise to print a \ or " which are special in string literal you have to escape it with another \ which gives us \\ and \"

What does a backslash mean in a string literal?

The backslash is used to escape special (unprintable) characters in string literals. \n is for newline, \t for tab, \f for a form-feed (rarely used) and several more exist.

When you give the string literal "\0" you effectively denote a string with exactly one character which is the (unprintable) NUL character (a 0-byte). You can represent this as \0 in string literals. The same goes for \1 (which is a 1-byte in a string) etc.

Actually, the \8 and \9 are different because after a backslash you have to denote the value of the byte you want in octal notation, e. g. using digits 07 only. So effectively, the backslash before the 8 and before the 9 has no special meaning and \8 results in two characters, namely the backslash verbatim and the 8 as a digit verbatim.

When you now print the representation of such a string literal (e. g. by having it in a list you print), then the Python interpreter recreates a representation for the internal string (which is supposed to look like a string literal). This is not the string contents, but the version of the string as you can denote it in a Python program, i. e. enclosed in quotes and using backslashes to escape special characters. The Python interpreter doesn't represent special characters using the octal notation, though. It uses the hexadecimal notation instead which introduces each special character with a \x followed by exactly two hexadecimal characters.

That means that \0 becomes \x00, \1 becomes \x01 etc. The \8, as mentioned, is in fact the representation of two characters, namely the backslash and the digit 8. The backslash is then escaped by the Python interpreter to a double backslash \\, and the 8 is appended as normal character.

The input \10 is the character with value 8 (because octal 10 is decimal 8 and also hexadecimal 8, look up octal and hexadecimal numbers to learn about that). So the input \10 becomes \x08. The \11 is the character with value 9 which is a tab character for which a special notation exists, that is \t.

What does back slash \ really mean?

The backslash \ is a character, just like the letter A, the comma ,, and the number 4. In some programming languages, notably C and its descendants (and maybe ancestors), it is used inside a string or character literal to escape other characters. For instance, '\a' represents the bell character, and will produce a beep from the computer if you print it (printf("%c", '\a')).

As a C-language escape character, it is largely a human construct allowed by the compiler so humans can express, e.g., the bell character. The compiled code simply stores the character — a byte with the value 7. Just to be absolutely clear, it does not store a \ followed by an a.

Under other contexts, the backslash means something to the program at runtime. The most well-known instance of this is regular expression syntax, in which a backslash escape other characters in order to either give them special meaning or take away a special meaning they might have. For example, grep '\<foo\>' file.txt will locate lines with the word foo in file.txt. In this case the backslashes really are there at runtime, and are interpreted by the program as escapes for the < and > that follow them. In this case, \< and \> don't represent characters at all; they denote a zero-width match against the beginning and end of a word, respectively.

The backslash character in Regex for Python

Python only recognizes some sequences starting with \ as escape sequences. For example \d is not a known escape sequence so for this particular case there is no need to escape the backslah to keep it there.

(In Python 3.6) "\d" and "\\d" are equivalent:

>>> "\d" == "\\d"
True
>>> r"\d" == "\\d"
True

Here is a list of all the recognized escape sequences: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals

What is the backslash in a Clojure REPL?

It means the character(s) following should be interpreted as a literal.

  • Characters - preceded by a backslash: \c. \newline, \space, \tab, \formfeed, \backspace, and \return yield the corresponding characters.
  • Unicode characters - are represented with \uNNNN as in Java.
  • Octals - are represented with \oNNN.

See the following links for more info.

  • Reading Clojure Characters: \ - Character literal
  • The Reader: Literals

How do I write a backslash (\) in a string?

The backslash ("\") character is a special escape character used to indicate other special characters such as new lines (\n), tabs (\t), or quotation marks (\").

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string:

var s = "\\Tasks";
// or
var s = @"\Tasks";

Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

Generally speaking, most C# .NET developers tend to favour using the @ verbatim strings when building file/folder paths since it saves them from having to write double backslashes all the time and they can directly copy/paste the path, so I would suggest that you get in the habit of doing the same.


That all said, in this case, I would actually recommend you use the Path.Combine utility method as in @lordkain's answer as then you don't need to worry about whether backslashes are already included in the paths and accidentally doubling-up the slashes or omitting them altogether when combining parts of paths.

How does Python interpret backslash in string?

From your follow-up comment:

What puzzled me is in my example, it doesn't escape. Single backslash produces double backslashes. Double backslashes produce Double backslashes. Triple backslashes produce quadruple backslashes.....

To be clear: your first output is a string with one backslash in it. Python displays two backslashes in its representation of the string.

When you input the string with a single backslash, Python does not treat the sequence \] in the input as any special escape sequence, and therefore the \ is turned into an actual backslash in the actual string, and the ] into a closing square bracket. Quoting from the documentation linked by Klaus D.:

Unlike Standard C, all unrecognized escape sequences are left in the string unchanged, i.e., the backslash is left in the result. (This behavior is useful when debugging: if an escape sequence is mistyped, the resulting output is more easily recognized as broken.)

When you input the string with a double backslash, the sequence \\ is an escape sequence for a single backslash, and then the ] is just a ].

Either way, when Python displays the string back to you, it uses \\ for the single actual backslash, because it does not look ahead to determine that a single backslash would work - the backslash always gets escaped.


To go into a little more detail: Python doesn't care about how you specified the string in the first place - it has a specific "normalized" form that depends only on what the string actually contains. We can see this by playing around with the different ways to quote a string:

>>> 'foo'
'foo'
>>> "foo"
'foo'
>>> r'foo'
'foo'
>>> """foo"""
'foo'

The normalized form will use double quotes if that avoids escape sequences for single quotes:

>>> '\'\'\''
"'''"

But it will switch back to single quotes if the string contains both types of quote:

>>> '\'"'
'\'"'
>>> "'\"'
'\'"'

(Exercise: how many characters are actually in this string, and what are they? How many backslashes does the string contain?)


It contains two characters - a single-quote and a double-quote - and no backslashes.

Backslash symbol \ in R environment within the string block

In standard R strings the backslash is the escape symbol and hence it also needs to be escaped itself. So you need "\\alpha" to denote the string \alpha. However, wehn you use \Sexpr{} you need to escape twice, once for reading and once for writing. Thus, you need "\\\\alpha" in that case.

If you write German umlaut characters it becomes even worse because the " also has to be escaped, e.g., sch\\\\\"on" to write schön in LaTeX. But if you don't use this very often, it's not worth trying to remeber it. Instead apply the strategy from fortunes::fortune(365):

When in doubt, keep adding slashes until it works.

-- Joran Elias (on how to escape a backslash in R)

   Stackoverflow (March 2015)

JavaScript backslash (\) in variables is causing an error

The backslash (\) is an escape character in Javascript (along with a lot of other C-like languages). This means that when Javascript encounters a backslash, it tries to escape the following character. For instance, \n is a newline character (rather than a backslash followed by the letter n).

In order to output a literal backslash, you need to escape it. That means \\ will output a single backslash (and \\\\ will output two, and so on). The reason "aa ///\" doesn't work is because the backslash escapes the " (which will print a literal quote), and thus your string is not properly terminated. Similarly, "aa ///\\\" won't work, because the last backslash again escapes the quote.

Just remember, for each backslash you want to output, you need to give Javascript two.



Related Topics



Leave a reply



Submit