How to use the ‘r’ keyword to specify a raw string In Python

In python, the ‘r’ keyword can be used to indicate a raw string to the compiler. This is rather helpful in special cases like taking input from the user.

Python3




# Using single quote
 
print(r"This is '(single quote)'")
 
# Using double quote
 
print(r'This is "(double quote)"')
 
# Using both single and double quote
 
print(r"These are '(single quote)' and " r'"(double quote)"')


Output:

This is '(single quote)'
This is "(double quote)"
These are '(single quote)' and "(double quote)"


How to Escape Quotes From String in Python

The single or double quotation is a unique character that can be used in a Python program to represent a string by encapsulating it in matching quotes. When the quotes are used inside a string contained in similar quotes it confuses the compiler and as a result, an error is thrown. 

This article will demonstrate how to escape quotes from a string in Python.

Similar Reads

Escape from single quotes in a string in Python

Wrong way:...

Escape from double quotes in a string in Python

...

Escape using triple quote

...

Using the ‘r’ keyword to specify a raw string

...