How to Print a Raw String in Python 


               


How to Print a Raw String in Python



Python


In this article, we show how to print a raw string in Python.

So sometimes you may want to be able to print output exactly as you have typed it into a print() function.

Maybe you want to print out an escape character in Python that doesn't execute but just shows the raw characters.

Maybe you've created an HTML page and you want to print out the HTML tags without the tags being executed.

Raw strings solve this problem in Python.

To create a raw string in Python, you place an r befor the beginning quotation mark of the string.

A raw string completely ignores all escape characters. It prints the string exactly as entered.

In the following code below, we show examples of raw strings in Python.



So we've demonstrated a number of raw string examples.

In the first example, we use the newline (\n) escape character. However, with raw strings, escape characters are simply shown but not executed. So the output is shown with the newline escape character.

In the next example, we print out HTML title tags. If you were running this code on an web server in an HTML document, it would print the title tags on the HTML document (if run without a raw string). With a raw string, it would print out the actual title tags.

In the last example, we have another escape character, a tab escape character. Again, since it's within a raw string, it simply prints out the string and the escape character isn't executed.

And this is how to print raw strings in Python.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...