![]() |
|
[Tut] How to Do a Backslash in Python? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Do a Backslash in Python? (/thread-95007.html) |
[Tut] How to Do a Backslash in Python? - xSicKxBot - 05-12-2020 How to Do a Backslash in Python? The Python backslash ( Code: '\'
Try it yourself in our interactive Python shell (just click “Run”): https://repl.it/@finxter/backslash?lite=true The backslash Code: \Code: 'n'Code: '\n'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: ![]() Code: print('\\')
>>> \This is exactly what we want to accomplish. the first character Code: \Code: \Code: \ |