Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Do a Backslash in Python?

#1
How to Do a Backslash in Python?

The Python backslash ('\') is a special character that’s used for two purposes:

  1. The Python backslash can be part of a special character sequence such as the tab character '\t', the newline character '\n', or the carriage return '\r'.
  2. The Python backslash can escape other special characters in a Python string. For example, the first backslash in the string '\\n' escapes the second backslash and removes the special meaning so that the resulting string contains the two characters '\' and 'n' instead of the special newline character '\n'.

Try it yourself in our interactive Python shell (just click “Run”):

The backslash \ is an escape character–if used in front of another character, it changes the meaning of this character. For example, the character 'n' is just that a simple character, but the character '\n' (yes, it’s one character consisting of two symbols) is the new line character. We say that it is escaped.

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:


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.

The correct way of accomplishing this is to escape the escape character itself:


print('\\')
>>> \

This is exactly what we want to accomplish. the first character \ escapes the second character \ and therefore removes its meaning. The second character \ is therefore interpreted as a simple backslash.



https://www.sickgaming.net/blog/2020/05/...in-python/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
Microsoft - 4 new Flipgrid features ampl...
Last Post: xSicKxBot
Today 04:57 AM
» Replies: 0
» Views: 0
iOS 11.4 - 11.4.1 Jailbreak (Electra Jai...
Last Post: WilliamAsymn
Yesterday 11:21 PM
» Replies: 1
» Views: 804
(Indie Deal) RE8, Frontier, Slitherine, ...
Last Post: xSicKxBot
Yesterday 08:49 PM
» Replies: 0
» Views: 4
Microsoft - New hardware from Designed f...
Last Post: xSicKxBot
Yesterday 08:49 PM
» Replies: 0
» Views: 2
News - Guide: 30 Upcoming Nintendo Switc...
Last Post: xSicKxBot
Yesterday 08:49 PM
» Replies: 0
» Views: 3
News - Fortnite Skin Leak Roundup: Spide...
Last Post: xSicKxBot
Yesterday 08:48 PM
» Replies: 0
» Views: 4
kernel-debug-modules-4.18.0-305.25.1.el8...
Last Post: WilliamAsymn
Yesterday 04:14 PM
» Replies: 1
» Views: 34
Microsoft - Full-time food and travel vl...
Last Post: xSicKxBot
Yesterday 02:35 PM
» Replies: 0
» Views: 3
News - No Gravity Games Is Giving Away 1...
Last Post: xSicKxBot
Yesterday 02:34 PM
» Replies: 0
» Views: 4
News - Steam Autumn Sale Is Live Now: Ch...
Last Post: xSicKxBot
Yesterday 02:34 PM
» Replies: 0
» Views: 4

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com



Discord Server © SickGaming.net 2012-2021