How do you escape escape sequence in python?


Escape Characters

To insert characters that are illegal in a string, use an escape character.

An escape character is a backslash \ followed by the character you want to insert.

An example of an illegal character is a double quote inside a string that is surrounded by double quotes:

Example

You will get an error if you use double quotes inside a string that is surrounded by double quotes:

txt = "We are the so-called "Vikings" from the north."

Try it Yourself »

To fix this problem, use the escape character \":

Example

The escape character allows you to use double quotes when you normally would not be allowed:

txt = "We are the so-called \"Vikings\" from the north."

Try it Yourself »

Other escape characters used in Python:

CodeResultTry it
\' Single Quote Try it »
\\ Backslash Try it »
\n New Line Try it »
\r Carriage Return Try it »
\t Tab Try it »
\b Backspace Try it »
\f Form Feed
\ooo Octal value Try it »
\xhh Hex value Try it »



How do you escape escape sequence in python?

Escape sequences allow you to include special characters in strings. To do this, simply add a backslash (\) before the character you want to escape.

For example, imagine you initialized a string with single quotes:

s = 'Hey, whats up?'
print(s)

Output:

Hey, whats up?

But if you include an apostrophe without escaping it, then you will get an error:

s = 'Hey, what's up?'
print(s)

Output:

  File "main.py", line 1
    s = 'Hey, what's up?'
                   ^
SyntaxError: invalid syntax

To fix this, just escape the apostrophe:

s = 'Hey, what\'s up?'
print(s)

To add newlines to your string, use \n:

print("Multiline strings\ncan be created\nusing escape sequences.")

Output:

Multiline strings
can be created
using escape sequences.

An important thing to remember is that, if you want to include a backslash character in a string, you will need to escape that. For example, if you want to print a directory path in Windows, you'll need to escape each backslash in the string:

print("C:\\Users\\Pat\\Desktop")

Output:

C:\Users\Pat\Desktop

Raw strings

A raw string can be used by prefixing the string with r or R, which allows for backslashes to be included without the need to escape them. For example:

print(r"Backslashes \ don't need to be escaped in raw strings.")

Output:

Backslashes \ don't need to be escaped in raw strings.

But keep in mind that unescaped backslashes at the end of a raw string will cause and error:

print(r"There's an unescaped backslash at the end of this string\")

Output:

  File "main.py", line 1
    print(r"There's an unescaped backslash at the end of this string\")
                                                                      ^
SyntaxError: EOL while scanning string literal

Common escape sequences

Escape SequenceMeaning
\ Backslash (\)
' Single quote (')
" Double quote (")
\n ASCII Linefeed (adds newline)
\b ASCII Backspace

A full list of escape sequences can be found here in the Python docs.


Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

EDIT: The problem is actually how print works with lists & strings. It prints the representation of the string, not the string itself, the representation of a string containing just a backslash is '\\'. So findall is actually finding the single backslash correctly, but print isn't printing it as you'd expect. Try:

>>> print(re.findall(r'\\',"i am \\nit")[0])
\

(The following is my original answer, it can be ignored (it's entirely irrelevant), I'd misinterpreted the question initially. But it seems to have been upvoted a bit, so I'll leave it here.)

The r prefix on a string means the string is in "raw" mode, that is, \ are not treated as special characters (it doesn't have anything to do with "regex").

However, r'\' doesn't work, as you can't end a raw string with a backslash, it's stated in the docs:

Even in a raw string, string quotes can be escaped with a backslash, but the backslash remains in the string; for example, r"\"" is a valid string literal consisting of two characters: a backslash and a double quote; r"\" is not a valid string literal (even a raw string cannot end in an odd number of backslashes). Specifically, a raw string cannot end in a single backslash (since the backslash would escape the following quote character).

But you actually can use a non-raw string to get a single backslash: "\\".

What is the use of '\ t escape sequence?

For example, you can use escape sequences to put such characters as tab, carriage return, and backspace into an output stream. ... Escape sequences..

What is the '\ n escape character?

In particular, the \n escape sequence represents the newline character. A \n in a printf format string tells awk to start printing output at the beginning of a newline.