How to Use Python Raw String?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Python strings become raw strings when they are prefixed with r or R, such as r'...' and R'...'. Raw strings treat backslashes () as literal characters.

Raw strings are useful for strings with a lot of backslashes, like regular expressions or directory paths.

Python uses the backslash () to indicate the start of escape sequences to represent special characters like tabs and newlines. As an example

Example:

Output:

The first string str includes one new-line character, and the second raw string raw_str '\n' itself instead of adding a newline

We can also use R instead of r:

It will produce the same results as above

Output:

What is a Raw String

Python raw strings are created by prefixing string literals with 'r' or 'R'. Raw Python strings treat backslashes () as literals. Backslashes are useful when we don't want them treated as escape characters in a string.

Where can Python Raw Strings be Used?

Raw strings are typically used when you require the raw string itself, not the processed version of it. A SyntaxError will be thrown if your normal string contains an invalid escape character, such as \x.

A regular expression is one of the most common uses of raw strings. Due to the high number of backslashes in regular expressions, it is easier to read them as raw strings.

How to Assign Values to Raw String in Python?

Raw String assignments are performed using the = operator. From the source operand, bytes are copied to the left-hand variable, which must be a raw string. An example of raw string assignment is shown below

s = r"\Hi";

We can also you single or triple quotes to assign a raw string

How to Update Python Raw String?

To update the python raw string we can use the replace method in python. In the replace method, the existing substring is replaced with a new one. Here is an example to help us understand

Example

Output:

In this way, we can easily replace and update the raw strings

Invalid Raw Strings

A raw string may not be valid in all cases. Raw strings containing only a backslash are invalid. Using raw strings that end with an odd number of backslashes is also invalid.

Here is an example:

When trying to execute the above code we will get Syntax Error: EOL while scanning string literal

Learn More

To learn more about strings have a look at these articles

Strings in Python

F String In Python

Conclusion

  • Python Raw strings generally contain the words within quotation marks, and the literal 'r' is added as a prefix, then we assign them to variables.
  • A big difference between the Python raw string and the String is that in the Python raw string, we write the word in quotation marks, and we print it out.
  • It is really useful to write regex expressions from raw strings.