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. Show
Python Raw StringLet’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.
Output:
Let’s see how raw string helps us in treating backslash as a normal character.
Output:
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.
If you are on Python console and create a raw-string like below.
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 QuotesWhen 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:
Let’s look at some of the valid raw string examples with quotes.
Output:
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 This article describes the following contents.
Escape sequencesIn 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
Raw strings treat backslashes as literal charactersStrings prefixed with
There is no special type for raw
strings; it is just a string, which is equivalent to a regular string with backslashes represented by
In a normal string, an escape sequence is considered to be one character, but in a raw string, backslashes are also counted as characters.
Windows pathsUsing the raw string is useful when representing a Windows path as a string. Windows paths are separated by backslashes
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.
Convert normal strings to raw strings with repr()Use the built-in function
The string returned by
Using slices, you can get the string equivalent to the raw string.
Raw strings cannot end with an odd number of backslashesSince backslashes escape the trailing
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.
|