1

In Python 3 I am trying to make a set containing a backslash as an element:

a = {"/", "\"}

But I noticed that as soon as I closed the bracket after "\", the bracket became blue. I, learned that \ is an "escape" character used in things like \r, \t etc. But I want to take a backslash as a single piece of string. How to prevent this problem?

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

2 Answers2

2

You need to escape the \ by also using a backslash. Therefore you will need two \\

Your string will then become a={"/","\\"}

0

In Python strings, the backslash "" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return. you can use this for \ and /:

>>> print('apple\torange')
apple   orange 
>>> print('apple\norange')
apple
orange 
>>> print('\\')
\
>>> print('/')
/
Salio
  • 1,058
  • 10
  • 21