Raw strings in python: Explanation with examples :

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. Python raw strings are useful for writing regular expressions and for using with SQL parsers.

Python raw strings are prefixed with ‘r’ or ‘R’. Prefix a string with ‘R’ or ‘r’ and it will be treated as a raw string. Let me show you with an example :

python raw string

You can see that the first string dummy_str includes one new-line and the second raw string raw_dummy_str also include one new-line character. But the new line was printed as ‘\n’ for the second string.
python raw string

Instead of r, we can also use R :

The output will be the same as above :
python raw string

Using raw string with different string literals :

We know that in python, we have multiple ways to write a string literal. We can use either a single quote, double quotes, double-triple quotes or single-triple quotes for a string literal. We can convert any of these string types to raw as well. Add one ‘R’ or ‘r’ to the start of the string and that’s it. Let me show you an example :

python raw string

python raw string

In the above example,
raw_str_one is a raw string with a single quote.
raw_str_two is a raw string with double quotes.
raw_str_three is a raw string with double triple quotes. This is a multiline string.
raw_str_four is a raw string with single triple quotes. This is also a multiline string.
Here, we are using ‘R’ for all raw strings but you can also use ‘r’ instead.

Where raw string is used :

Normally raw strings are used where you want the actual raw string that is given, not the processed version of that string. For example, if your string contains any invalid escape character like \x, it will throw one SyntaxError.

One common use case of the raw string is a regular expression. Regular expressions are represented as strings and since they contain a lot of backslashes, using them as raw makes more readable.

Invalid raw strings :

Not all raw strings are valid. A raw string that contains only a single backslash is not valid. Similarly, raw strings with an odd number of ending backslash are also not valid.

python raw string

If you try to print any of the string above, it will throw one exception SyntaxError: EOL while scanning string literal.
You can change the program like below, but the output will be different as well :

python raw string

Conclusion :

This python tutorial introduced you to raw string and how they behave. We have also learned how to create different types of raw string, print raw string and invalid raw string with examples. Raw strings are really useful for writing regex expressions. Go through the examples explained above and drop one comment below if you have any queries.


You might also like :

Categories: python

Leave a Reply

Your email address will not be published. Required fields are marked *