05-12-2020, 10:19 AM
How to Do a Backslash in Python?
<div><p><strong>The Python backslash (<code>'\'</code>) is a special character that’s used for two purposes:</strong></p>
<ol>
<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>
<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>
</ol>
<p>Try it yourself in our interactive Python shell (just click “Run”
:</p>
<p> <iframe 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>
<p>The backslash <code>\</code> is an <a href="https://en.wikipedia.org/wiki/Escape_character">escape character</a>–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’s one character consisting of two symbols) is the new line character. We say that it is <em>escaped</em>.</p>
<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’s an example:</p>
<figure class="wp-block-image"><img 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/uplo...00x102.png 300w, https://blog.finxter.com/wp-content/uplo...68x262.png 768w, https://blog.finxter.com/wp-content/uplo...fik-15.png 1619w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>We want to print a string consisting of a single backslash, but the backslash escapes the end of string literal \’. Hence, the interpreter believes the string was never closed and throws an error.</p>
<p>The correct way of accomplishing this is to escape the escape character itself:</p>
<figure class="wp-block-image"><img 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/uplo...300x83.png 300w, https://blog.finxter.com/wp-content/uplo...68x213.png 768w, https://blog.finxter.com/wp-content/uplo...fik-16.png 1656w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<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('\\')
>>> \</pre>
<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>
</div>
https://www.sickgaming.net/blog/2020/05/...in-python/
<div><p><strong>The Python backslash (<code>'\'</code>) is a special character that’s used for two purposes:</strong></p>
<ol>
<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>
<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>
</ol>
<p>Try it yourself in our interactive Python shell (just click “Run”
![Wink Wink](https://www.sickgaming.net/images/smilies/wink.png)
<p> <iframe 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>
<p>The backslash <code>\</code> is an <a href="https://en.wikipedia.org/wiki/Escape_character">escape character</a>–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’s one character consisting of two symbols) is the new line character. We say that it is <em>escaped</em>.</p>
<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’s an example:</p>
<figure class="wp-block-image"><img 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/uplo...00x102.png 300w, https://blog.finxter.com/wp-content/uplo...68x262.png 768w, https://blog.finxter.com/wp-content/uplo...fik-15.png 1619w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>We want to print a string consisting of a single backslash, but the backslash escapes the end of string literal \’. Hence, the interpreter believes the string was never closed and throws an error.</p>
<p>The correct way of accomplishing this is to escape the escape character itself:</p>
<figure class="wp-block-image"><img 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/uplo...300x83.png 300w, https://blog.finxter.com/wp-content/uplo...68x213.png 768w, https://blog.finxter.com/wp-content/uplo...fik-16.png 1656w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<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('\\')
>>> \</pre>
<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>
</div>
https://www.sickgaming.net/blog/2020/05/...in-python/