Open In App
Related Articles

Python Raw Strings

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In Python, a raw string is a special type of string that allows you to include backslashes (\) without interpreting them as escape sequences. In this article, we will see how to take care of a backslash combined with certain alphabet forms literal characters which can change the entire meaning of the string using Python.

Input: r'Python\nis\easy\to\learn'
Output: Python\nis\easy\to\learn
Explanation: As we can see that the string is printed as raw, and it neglected all newline sign(\n) or tab space (\t).

When to use raw strings in Python?

Raw strings are particularly useful when working with regular expressions, as they allow you to specify patterns that may contain backslashes without having to escape them. They are also useful when working with file paths, as they allow you to specify paths that contain backslashes without having to escape them. Raw strings can also be useful when working with strings that contain characters that are difficult to type or read, such as newline characters or tabs. In general, raw strings are useful anytime you need to specify a string that contains characters that have special meaning in Python, such as backslashes, and you want to specify those characters literally without having to escape them.

Ways to convert String to Raw String

Here, we will see the various ways to use the string text to convert Python r String, without its meaning getting changed.

Python Raw Strings using r Before String Declaration

In this approach, we will see one of the most common ways to solve this issue, i.e., convert the regular string to raw string. What we need to do is just put the alphabet r before defining the string. Here r means raw string which will display the text in quotes as it is.

Syntax

string_text = r'#Text to be inserted in the string'

Raw Strings to Handle Text

In this example, we defined a string using r before quotes and assigned it to a variable. Further, we have printed that variable to see the result.

Python

# Python program to use raw strings
 
# Define string with r before quotes
# and assign to variable
s1 = r'Python\nis\easy\to\learn'
 
print(s1)

                    

Output

Python\nis\easy\to\learn

Raw Strings to Handle File Path on Windows 

In this example, we defined a string, which is basically a file path of Windows, using r before quotes and assigned it to a variable. Further, we have printed that variable to see the result.

Python

# Python program to use raw strings
 
# Define string with r before quotes
# and assign to variable
s1 = r'c:\parent\tasks\new'
 
print(s1)

                    

Output

c:\parent\tasks\new

Get Python Raw Strings using Double Backslashes Instead of a Single Backslash

Another way to solve this problem of converting regular string is by using double backslashes ( \\ ) instead of a single backslash ( \ ). Thus, whenever we need to use a backslash while defining the string in Python, we have to use double backslashes ( \\ ). Through this technique, you can also handle one of the most common errors in Python, i.e., EOL while scanning string literal. 

Syntax

string_text = 'Python\\tis\\neasy\\to\\understand'

Compare backslash ( \ ) with double backslashes ( \\ )

In this example, we have defined two strings, string_text1 and string_text2, string_text1 by using a single backslash, and string_text2 by using double backslashes. Further, we have printed those variables to see the output.

Python3

string_text1 = 'Python\tis\neasy\to\learn'
 
# Print the string string_text1
print(string_text1)
 
# Define string with double backslash
# and assign to variable
string_text2 = 'Python\\tis\\neasy\\to\\learn'
 
print(string_text2)

                    

Output

Python    is
easy o\learn
Python\tis\neasy\to\learn

Get Python Raw Strings using Repr() function

We have also got for you one of the unique but a bit lengthy solutions as compared to the previous two solutions. In this solution, we will use the function repr(), which will generate the object orientation in string format, but it also gives single quotes around the text. Thus, we will truncate the quotes by using the slicing method.

Syntax

raw_string = repr(string_defined)[1:-1]

Example: In this example, we have defined the string. Now, we will return the object orientation in string format using function repr(), which will print the text with single quotes. Thus, we will truncate the quotes by using the slicing method and assigning it to a variable. Finally, print the variable to see the result.

Python

s = '\n'
 
# Return object orientation in string format
# and truncating quotes
raw_string = repr(s)[1:-1]
 
# Print the new string created
print(raw_string)
 
# Print the original string
print(s)
 
print(raw_string)

                    

Output

\n
\n

To Count occurrences of escape sequence in string

In this example, we will find the length of the string by using the Python len() function. Then, we replace backslash ( \ ) with double backslashes ( \\ ) in the string and now find the length of the update string. Later, we find the difference between the lengths of the updated string and the original string to calculate the count of escape sequences in the string.

Python

a='Python\nis\easy\to\learn'
 
length_with_escape=len(a)
b=a.replace("\\" , "\\\\")
 
# Get length of updated string using len() function
length_without_escape=len(b)
 
# Get count of escape sequence in Python
count_escape=length_without_escape-length_with_escape
 
print('Count of escape sequence: ',count_escape)

                    

Output

Count of escape sequence: 2

Through this example, we saw that if we do not use the appropriate fix for the string, it will result in a completely different output

Raw strings cannot end with an odd number of backslashes

If there are an odd number of backslashes \ at the end of the text, an error will happen because backslashes escape the trailing ” or “. 

Note: Always use an even number of backslashes.

Python3

# print(r'\\')
# It will print \\
 
print(r'\')
# SyntaxError: EOL while scanning string literal

                    

Output

  File "<ipython-input-32-aa21331d8cba>", line 4
print(r'\')
^
SyntaxError: EOL while scanning string literal

Fix SyntaxError: EOL while scanning string literal 

In this example, we will see how can we fix the most commonly occurring error, SyntaxError: EOL while scanning string literal. This error usually occurs when it encounters a single backslash before quotes. We can fix this issue by using double backslashes ( \\ ) instead of single backslash ( \ ) before quotes.

Python

# Python program to fix
# SyntaxError: EOL while scanning string literal
 
# Define string with double backslash
# and assign to variable
string_text2 = 'c:\\parent\\tasks\\new\\'
 
print(string_text2)

                    

Output

c:\parent\tasks\new\

Count occurrences of escape sequence in string

In this example, we will find the length of the string by using the Python len() function. Then, we replace backslash ( \ ) with double backslashes ( \\ ) in the string and now find the length of the update string. Later, we find the difference between the lengths of the updated string and the original string to calculate the count of escape sequences in the string.

Python

a='Python\nis\easy\to\learn'
 
length_with_escape=len(a)
b=a.replace("\\" , "\\\\")
 
# Get length of updated string using len() function
length_without_escape=len(b)
 
# Get count of escape sequence in Python
count_escape=length_without_escape-length_with_escape
 
print('Count of escape sequence: ',count_escape)

                    

Output

Count of escape sequence: 2

Through this example, we saw that if we do not use the appropriate fix for the string, it will result in a completely different output.



Last Updated : 28 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments