Python raw strings : Explanation with examples
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 an example :
dummy_str = "This is a \n normal string" print(dummy_str) raw_dummy_str = r"This is a \n raw string" print(raw_dummy_str)
It will print the below output :
This is a normal string This is a \n 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.
Instead of r, we can also use R :
raw_dummy_str = R"This is a \n raw string" print(raw_dummy_str)
The output will be the same as above :
Using a 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 :
raw_str_one = R"This is a \t raw string with single quote" raw_str_two = R"This is another \n raw string with double quotes" raw_str_three = R"""This is a \t multiline raw string with double triple quotes""" raw_str_four = R'''This is a \t raw string with single triple quotes''' print(raw_str_one) print(raw_str_two) print(raw_str_three) print(raw_str_four)
It will print the below output :
This is a \t raw string with single quote This is another \n raw string with double quotes This is a \t multiline raw string with double triple quotes This is a \t raw string with single triple quotes
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.
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 :
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.