{"id":112761,"date":"2020-05-11T19:00:00","date_gmt":"2020-05-11T19:00:00","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=4553"},"modified":"2020-05-11T19:00:00","modified_gmt":"2020-05-11T19:00:00","slug":"how-to-do-a-backslash-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/05\/11\/how-to-do-a-backslash-in-python\/","title":{"rendered":"How to Do a Backslash in Python?"},"content":{"rendered":"<p><strong>The Python backslash (<code>'\\'<\/code>) is a special character that&#8217;s used for two purposes:<\/strong><\/p>\n<ol>\n<li><strong>The Python backslash can be part of a special character sequence such as the tab character <code>'\\t'<\/code>, the newline character <code>'\\n'<\/code>, or the carriage return <code>'\\r'<\/code>.<\/strong><\/li>\n<li><strong>The Python backslash can escape other special characters in a Python string. For example, the first backslash in the string <code>'\\\\n'<\/code> escapes the second backslash and removes the special meaning so that the resulting string contains the two characters <code>'\\'<\/code> and <code>'n'<\/code> instead of the special newline character <code>'\\n'<\/code>. <\/strong><\/li>\n<\/ol>\n<p>Try it yourself in our interactive Python shell (just click &#8220;Run&#8221;):<\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@finxter\/backslash?lite=true\" scrolling=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\" width=\"100%\" height=\"600px\" frameborder=\"no\"><\/iframe> <\/p>\n<p>The backslash <code>\\<\/code> is an <a href=\"https:\/\/en.wikipedia.org\/wiki\/Escape_character\">escape character<\/a>&#8211;if used in front of another character, it changes the meaning of this character. For example, the character <code>'n'<\/code> is just that a simple character, but the character <code>'\\n'<\/code> (yes, it&#8217;s one character consisting of two symbols) is the new line character. We say that it is <em>escaped<\/em>.<\/p>\n<p>So how do we define a string consisting of the backslash? The problem is that if we use the backslash, Python thinks that the character that follows the backslash is escaped. Here&#8217;s an example:<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-15-1024x350.png\" alt=\"\" class=\"wp-image-4554\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-15-1024x350.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-15-300x102.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-15-768x262.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-15.png 1619w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>We want to print a string consisting of a single backslash, but the backslash escapes the end of string literal \\&#8217;. Hence, the interpreter believes the string was never closed and throws an error.<\/p>\n<p>The correct way of accomplishing this is to escape the escape character itself:<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-16-1024x284.png\" alt=\"\" class=\"wp-image-4555\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-16-1024x284.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-16-300x83.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-16-768x213.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2019\/09\/grafik-16.png 1656w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">print('\\\\')\n>>> \\<\/pre>\n<p>This is exactly what we want to accomplish. the first character <code>\\<\/code> escapes the second character <code>\\ <\/code>and therefore removes its meaning. The second character <code>\\<\/code> is therefore interpreted as a simple backslash.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Python backslash (&#8216;\\&#8217;) is a special character that&#8217;s used for two purposes: The Python backslash can be part of a special character sequence such as the tab character &#8216;\\t&#8217;, the newline character &#8216;\\n&#8217;, or the carriage return &#8216;\\r&#8217;. The Python backslash can escape other special characters in a Python string. For example, the first [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-112761","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=112761"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112761\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}