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.
raw strings are prefixed with ‘r’ or ‘R’. Let me show you with an example how it looks like :
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
So, you can see that the first string dummy_str includes one new-line and the second raw string raw_dummy_str doesn’t include that new-line. Instead, it printed out the string exactly same as it is including \n.
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 same as above :
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 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. Let me show you with 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)
The output is as below :
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
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 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.
raw_str_one = r"\" raw_str_two = r"xyz\" raw_str_three = r"xyz\\\"
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 :
raw_str_one = r"\\" raw_str_two = r"xyz\\" raw_str_three = r"xyz\\\\" print(raw_str_one) print(raw_str_two) print(raw_str_three)
\\ xyz\\ xyz\\\\
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. Go through the examples explained above and drop one comment below if you have any queries.
You might also like :
- Python String
- Python Common String methods
- What is Docstring and how to write docsrting in python
- How to use Hashlib to encrypt a string in python
- Python program to sort words of a string in alphabetical order
- Python 3 program to check if a string is pangram or not