Updated April 4, 2023
Introduction to Python Raw String
The Strings is one of the important Functions to be learned while we are using Python. The Strings play a key role in writing the code for a word. The string is generally a data type used for writing the function in Python. It is used for integer and floating. We can also use it for the character types also. The strings generally consist of numbers and words. For example, if we want to get the letters from Particular words, we use the String function. The String function is generally written in quotation marks. The word is enclosed in the Quotation. The Python generally takes the double quotation as a single quotation. Like we give a value to the variable, we can also assign a string to the variable. We can generally access the string using indexing. Indexing means getting only a particular range of characters. The Slicing is done to the string using an operator called the Slicing operator. In this topic, we are going to learn about Python Raw String.
Generally, the Python Raw strings are written with a string Letter ‘r’ or ‘R’. This ‘r’ acts as a prefix to the string. The Python raw string treats the backslash (/) as a single character.
Syntax:
The Syntax for Python raw string is:
Print (r 'word')
How do raw strings used in Python?
Here we observe that the backslash used in the Variable makes the word into two parts. One word as Today is and the other word as of Thursday. The Backslash prints the words next to it in the next line. But we want the entire word printed in a single line. So for this, we use the Python raw string, i.e. we use the r as the prefix for the word. The ‘r’ letter treats the backslash as the normal character. When we use the raw string, the output will be as shown in the below Example.
Example:
Suppose we want to print a String (‘Today is \n Thursday’). First, assign the string to a variable. Then print the variable by using the print command.
s = 'Todayis\nThursday';
print(s);
And the output will be as follows.
When we use the raw string, the code will be as below.
raw_s = 'Todayis\nThursday';
print(s)
And the output will be as follows.
If we use other letters in place of r, python simple throws an error which is shown below.
raw_s1 = 'Todayis\xThursday';
print(raw_s1)
And the output will be as follows.
The letter \x does have any predefined function. The error occurred because we have not used the prefix r, which is generally used as the raw string. If we use the raw string then the output will be as follows.
raw_s2 = r'Todayis\xThursday';
print(raw_s2)
And the output will be as follows.
When we give the only backslash as the input to a variable with a raw string, then the output will be as
Here when we observe the python only takes the words inside the raw string in a variable and displays the output.
If they are not properly terminated, it simply gives an error.
Where we can use the Python Raw strings?
Raw Strings are generally used when we want to print the given string in the same line. If it contains the invalid character ‘\x’, it simply throws an error. At that time to print the given input without throwing an error, we use the Raw strings to print the given statement. We generally use the raw strings in the regular expression. Generally, the regular expressions are written as strings. This regular expression contains the words with the backslashes. To print those regular expressions without throwing an error, we will use the raw strings to print the given statements without throwing an error.
Non Printable characters with Backslash
Non-Printable character | Description |
\a | It is used to alert |
\b | It is used to give backspace |
\n | It is used to go to the new Line |
\s | It is used to give space in the statement |
\t | It is used to select the tab |
\e | It is used to escape |
Program:
s = 'Todayis\nThursday';
print(s);
raw_s = r'Today is Thursday';
print(raw_s)
raw_s1 ='Today is Thursday';
print(raw_s1)
raw_s2 = r'Today is Thursday';
print(raw_s2)
Output:
Assigning values to the strings
The strings are generally written in single or double quotes. Now to use the substrings in the given string, we will use the square brackets, which is used for the slicing. This can be explained easily with the below example. Here we can see in the below example that first, the values are assigned to two variables Var1 and Var2. Now we want to only print the substrings. To get the substrings, we have to mention their range in the brackets. In the below example, we want the substring with range 1:5 it means it prints the letters which are at the indices of 1 to 5.
Program
Var1= 'Today is Friday';
Var2= "Tomorrow is Saturday";
print('Var1[0]:',Var1[0]);
print('Var2[1:5]:',Var2[1:5]);
Output:
The output will be as shown below
Updating the string
We can update the given string with another string or add the given string with the new string. Here we are just updating the string. This can be explained easily with the below example. Here we have a String as ‘Today is’ now we are adding that string with the new string ‘Saturday’.
Program
var1 = 'Today is'
print ("Updated String: - ", var1[:9] + 'Friday')
Output:
The output will be as follows
Key Points to Remember
- The term ‘\n’ means space.
- The main difference between the String and the Python raw string is in the normal string, the word is written in the quotes, and we use the print command to get the string printed.
- In the Python Raw string, we usually write the word within the quotes, and in addition to it, we add the literal ‘r’ as the prefix to it, and then we assign it to a variable.
- And print that variable.
- All the Raw strings must contain the literal ‘r’ at its prefix.
Recommended Articles
This is a guide to Python Raw String. Here we discuss How raw strings are used in Python with examples and Where we can use them. You may also have a look at the following articles to learn more –