What is meant by raw string in python?

Python raw string is created by prefixing a string literal with ‘r’ or ‘R’. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don’t want it to be treated as an escape character.

Python Raw String

Let’s say we want to create a string Hi\nHello in python. If we try to assign it to a normal string, the \n will be treated as a new line.

s = 'Hi\nHello'
print(s)

Output:

Hi
Hello

Let’s see how raw string helps us in treating backslash as a normal character.

raw_s = r'Hi\nHello'
print(raw_s)

Output: Hi\nHello Let’s see another example where the character followed by backslash doesn’t have any special meaning.

>>> s = 'Hi\xHello'
  File "", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \xXX escape

We got the error because python doesn’t know how to decode ‘\x’ as it doesn’t have any special meaning. Let’s see how we can create the same string using raw strings.

>>> s = r'Hi\xHello'
>>> print(s)
Hi\xHello

If you are on Python console and create a raw-string like below.

>>> r'Hi\xHello'
'Hi\\xHello'

Don’t get confused with the output having two backslashes. It’s just to show it as a normal python string where backslash is being escaped.

Python Raw String and Quotes

When a backslash is followed by a quote in a raw string, it’s escaped. However, the backslash also remains in the result. Because of this feature, we can’t create a raw string of single backslash. Also, a raw string can’t have an odd number of backslashes at the end. Some of the invalid raw strings are:

r'\'  # missing end quote because the end quote is being escaped
r'ab\\\'  # first two backslashes will escape each other, the third one will try to escape the end quote.

Let’s look at some of the valid raw string examples with quotes.

raw_s = r'\''
print(raw_s)

raw_s = r'ab\\'
print(raw_s)

raw_s = R'\\\"'  # prefix can be 'R' or 'r'
print(raw_s)

Output:

\'
ab\\
\\\"

That’s all for a quick introduction of python raw string.

You can checkout complete python script and more Python examples from our GitHub Repository.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up

In Python, strings prefixed with r or R, such as r'...' and r"...", are called raw strings and treat backslashes \ as literal characters. Raw strings are useful when handling strings that use a lot of backslashes, such as Windows paths and regular expression patterns.

This article describes the following contents.

  • Escape sequences
  • Raw strings treat backslashes as literal characters
  • Convert normal strings to raw strings with repr()
  • Raw strings cannot end with an odd number of backslashes

Escape sequences

In Python, characters that cannot be represented in a normal string (such as tabs, line feeds. etc.) are described using an escape sequence with a backslash \ (such as \t or \n), similar to the C language.

  • 2. Lexical analysis - String and Bytes literals — Python 3.9.7 documentation

s = 'a\tb\nA\tB'
print(s)
# a b
# A B

Raw strings treat backslashes as literal characters

Strings prefixed with r or R, such as r'...' and r"...", are called raw strings and treat backslashes \ as literal characters. In raw strings, escape sequences are not treated specially.

rs = r'a\tb\nA\tB'
print(rs)
# a\tb\nA\tB

There is no special type for raw strings; it is just a string, which is equivalent to a regular string with backslashes represented by \\.

print(type(rs))
# 

print(rs == 'a\\tb\\nA\\tB')
# True

In a normal string, an escape sequence is considered to be one character, but in a raw string, backslashes are also counted as characters.

  • Get the length of a string (number of characters) in Python

print(len(s))
# 7

print(list(s))
# ['a', '\t', 'b', '\n', 'A', '\t', 'B']

print(len(rs))
# 10

print(list(rs))
# ['a', '\\', 't', 'b', '\\', 'n', 'A', '\\', 't', 'B']

Windows paths

Using the raw string is useful when representing a Windows path as a string.

Windows paths are separated by backslashes \, so if you use a normal string, you have to escape each one like \\, but you can write it as is with a raw string.

path = 'C:\\Windows\\system32\\cmd.exe'
rpath = r'C:\Windows\system32\cmd.exe'
print(path == rpath)
# True

Note that a string ending with an odd number of backslashes raises an error, as described below. In this case, you need to write it in a normal string or write only the trailing backslash as a normal string and concatenate it.

path2 = 'C:\\Windows\\system32\\'
# rpath2 = r'C:\Windows\system32\'
# SyntaxError: EOL while scanning string literal
rpath2 = r'C:\Windows\system32' + '\\'
print(path2 == rpath2)
# True

Convert normal strings to raw strings with repr()

Use the built-in function repr() to convert normal strings into raw strings.

  • Built-in Functions - repr() — Python 3.9.7 documentation

s_r = repr(s)
print(s_r)
# 'a\tb\nA\tB'

The string returned by repr() has ' at the beginning and the end.

print(list(s_r))
# ["'", 'a', '\\', 't', 'b', '\\', 'n', 'A', '\\', 't', 'B', "'"]

Using slices, you can get the string equivalent to the raw string.

s_r2 = repr(s)[1:-1]
print(s_r2)
# a\tb\nA\tB

print(s_r2 == rs)
# True

print(r'\t' == repr('\t')[1:-1])
# True

Raw strings cannot end with an odd number of backslashes

Since backslashes escape the trailing ' or ", an error will occur if there are an odd number of backslashes \ at the end of the string.

  • Design and History FAQ - Why can’t raw strings (r-strings) end with a backslash? — Python 3.9.7 documentation

# print(r'\')
# SyntaxError: EOL while scanning string literal

print(r'\\')
# \\

# print(r'\\\')
# SyntaxError: EOL while scanning string literal

What is raw string in Python?

Python raw string is created by prefixing a string literal with 'r' or 'R'. Python raw string treats backslash (\) as a literal character. This is useful when we want to have a string that contains backslash and don't want it to be treated as an escape character.

What does a raw string mean?

A raw string in programming allows all characters in a string literal to remain the same in code and in the material, rather than performing their standard programming functions. Raw strings are denoted with the letter r, or capital R, and might look something like this: R “(hello)”

What is the difference between string and raw string in Python?

Unlike a regular string, a raw string treats the backslashes ( \ ) as literal characters. Raw strings are useful when you deal with strings that have many backslashes, for example, regular expressions or directory paths on Windows.

What is raw string give an example?

raw strings are raw string literals that treat backslash (\ ) as a literal character. For example, if we try to print a string with a “\n” inside, it will add one line break. But if we mark it as a raw string, it will simply print out the “\n” as a normal character.