{"id":20774,"date":"2022-09-26T23:00:11","date_gmt":"2022-09-26T17:30:11","guid":{"rendered":"https:\/\/java2blog.com\/?p=20774"},"modified":"2022-09-26T23:00:11","modified_gmt":"2022-09-26T17:30:11","slug":"prefix-r-before-string-python","status":"publish","type":"post","link":"https:\/\/java2blog.com\/prefix-r-before-string-python\/","title":{"rendered":"Prefix r before String in Python"},"content":{"rendered":"<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#Prefix_r_Before_String_in_Python\">Prefix r Before String in Python<\/a><\/li><li><a href=\"#Conclusion\">Conclusion<\/a><\/li><\/ul><\/div>\n<blockquote><p>\n<span style=\"font-size: 1.2em\">&#x1f4a1; <strong>Quick Definition<\/strong><\/span><br \/>\nPrefix r before String denotes raw Strings in Python. Python raw string considers backslash (\\) as a literal character. This is generally preferred when you don&#8217;t want to treat backslash character as escape character.<br \/>\nHere is an example:<\/p>\n<pre code = \"python\" title = \"Raw Strings in Python\">\ns = r\"\\n \\t are escape characters in Python\"\nprint(s)    \n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"content-box-green\">\n\\n \\t are escape characters in Python\n<\/div>\n<\/blockquote>\n<p>Escape sequences provide an efficient way to convey some alternative meaning of a character to the compiler. Some common escape sequences include <code>\\n<\/code> for new line, <code>\\t<\/code> for horizontal tabs, and more.<\/p>\n<p>Python also supports such escape sequences and the Python interpreter uses their defined meanings when it encounters such sequences. <\/p>\n<p>For example, <\/p>\n<pre code = \"python\" title = \"Escape Sequences in Python\">\ns = \"java \\n2 blog\"\nprint(s)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\njava<br \/>\n2 blog\n<\/div>\n<p>In the above example, we use the <code>\\n<\/code> sequence to specify that a new line needs to be added.<\/p>\n<h2><span id=\"Prefix_r_Before_String_in_Python\">Prefix <code>r<\/code> Before String in Python<\/span><\/h2>\n<p><strong>Now there might be situations where we are required to display backslashes in the string and not want them to be understood as an escape sequence.<\/strong><\/p>\n<p><strong>For such situations, we use the prefix <code>r<\/code> before String in Python. This <code>r<\/code> prefix tells the interpreter that the given string is a raw string literal. Every character in such a string is taken as a string character and backslashes are not interpreted as escape sequences.**<\/strong><\/p>\n<p>See the code below.<\/p>\n<pre code = \"python\" title = \"Raw Strings in Python\">\ns = r\"java \\n2 blog\"\nprint(s)    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\njava \\n2 blog\n<\/div>\n<p>In the above example, we use the same string we did in the earlier used code but use the prefix <code>r<\/code> before string in Python. It tells the compiler that it is a raw string literal and thus the <code>\\n<\/code> escape sequence is interpreted as a string and no new line gets added to the string.<\/p>\n<section class=\"read-more-posts\">\n<div class=\"rm-header\">\n<h2>Further reading:<\/h2>\n<\/div>\n<div class=\"rm-wrap\">\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/escape-backslash-character-python\/\" target=\"_blank\" rel=\"noopener\">Escape Backslash Character in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/escape-backslash-character-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/div>\n<div class=\"rm-item\">\n<h5><a href=\"https:\/\/java2blog.com\/escape-quotes-in-python\/\" target=\"_blank\" rel=\"noopener\">Escape quotes in Python<\/a><\/h5>\n<div class=\"ex\">\n            <a href=\"https:\/\/java2blog.com\/escape-quotes-in-python\/\" target=\"_blank\" rel=\"noopener\">Read more \u2192<\/a>\n        <\/div>\n<\/p><\/div>\n<\/div>\n<\/section>\n<p>Now, there is a distinction in using the <code>r<\/code> prefix in Python 2 and Python 3. The former considers strings as ASCII-encoded letters whereas the latter expands on the character set and considers strings as a collection of Unicode characters.<\/p>\n<p>In Python 2, we use the prefix <code>u<\/code> if we wish to use Unicode characters in a string. We can use this along with the <code>r<\/code> prefix by combining them as <code>ur<\/code>. If we use the <code>ur<\/code> prefix before a string then we are specifying to the compiler that we are creating raw string literals with Unicode characters.<\/p>\n<p>For example,<\/p>\n<pre code = \"python\" title = \"Using the ur prefix\">\na = ur'java\\n2blog'\nprint a    \n<\/pre>\n<p>Output:<\/p>\n<div class=\"content-box-green\">\njava\\n2blog\n<\/div>\n<h2><span id=\"Conclusion\">Conclusion<\/span><\/h2>\n<p>To conclude this tutorial, we discussed the prefix <code>r<\/code> before string in Python. First, we discussed the significance of escape sequences. Then, we went on to demonstrate the use of raw string literals that can be created using the prefix <code>r<\/code> before string in Python. We also demonstrated the use of the <code>ur<\/code> prefix in Python 2 to create string literals with Unicode characters.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsPrefix r Before String in PythonConclusion &#x1f4a1; Quick Definition Prefix r before String denotes raw Strings in Python. Python raw string considers backslash (\\) as a literal character. This is generally preferred when you don&#8217;t want to treat backslash character as escape character. Here is an example: s = r&#8221;\\n \\t are escape [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[186,145],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20774"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=20774"}],"version-history":[{"count":7,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20774\/revisions"}],"predecessor-version":[{"id":20805,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/20774\/revisions\/20805"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=20774"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=20774"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=20774"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}