-4

I have this string:

pass__ = 'HSSSTS00008\4Tech'

However, printing this return this format:

'HSSSTS00008\x04Tech'

Therefore I'd like to remove the special character x and having the same value as pass__ after printing it, since this is a validation for an authentication layer.

Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
  • 6
    Why would you use a python keyword as a variable? And you could raw string to solve it, `pass = r'HSSSTS00008\4Tech'` that will print `'HSSSTS00008\\4Tech'` – Vineeth Sai Nov 08 '18 at 07:33
  • 1
    ``\4`` is not a backslash followed by digit 4. It's a character with the ASCII code 4. You need `'\\4'`. – DYZ Nov 08 '18 at 07:37
  • Thanks. But it's printing 'HSSSTS00008\\4Tech' , the result i want is the intial data contained in my variable which is 'HSSSTS00008\4Tech' so pass__ should return me 'HSSSTS00008\4Tech' since pass__ is a passkey – Jonas Amara Nov 08 '18 at 07:44
  • @JonasAmara the representation of ``\`` is ``\\``, so that's normal. `print(pass__)` will show what you expect. – Adam Smith Nov 08 '18 at 07:50
  • It is better to use forward slashes anyway for a [Windows path in Python](https://stackoverflow.com/questions/2953834/windows-path-in-python). – Karl Knechtel Aug 07 '22 at 04:02

3 Answers3

2

You can use raw strings, And please do not use pass as it as python keyword.

password = r'HSSSTS00008\4Tech'

Now printing that would give

'HSSSTS00008\\4Tech'
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
  • Thanks. But it's printing 'HSSSTS00008\\4Tech' , the result i want is the intial data contained in my variable which is 'HSSSTS00008\4Tech' so pass__ should return me 'HSSSTS00008\4Tech' – Jonas Amara Nov 08 '18 at 07:43
  • 3
    No, printing would **not** give `'HSSSTS00008\\4Tech'`, with double slash, it would give `'HSSSTS00008\4Tech'`, just as asked for. That is the whole point of the exercise, right? (Hower, just writing the variable name at the interpreter would return the double slash representation). – JohanL Nov 08 '18 at 07:43
2

The individual characters in your string aren't what you think they are. See this sample string below:

"ab\nc" -> "a", "b", "\n", "c"

Similarly

"HSSSTS00008\4Tech" -> "H", "S", "S", ..., "0", "8", "\x04", "T", ...

If you're trying to use the literal backslash followed by the literal 4, you should use a "raw string"

r"HSSSTS00008\4Tech" -> "H", "S", "S", ... "0", "8", "\", "4", ...
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

There are (at least) two different ways to achieve what you want, either using a raw string (as described by the other answers):

pass__ = r'HSSSTS00008\4Tech'

or it would be possible to write it using the escape sequence '\\' in a normal string:

pass__ = 'HSSSTS00008\\4Tech'

Both of these will generate the same string. When looking at the string representation, i.e., what you get if you just write the variable name in the interpreter or print the .__repr__() representation you will see:

>>> pass__
'HSSSTS00008\\4Tech'
>>> print(pass__.__repr__())
'HSSSTS00008\\4Tech'

Thus, the program representation of the string is with double backslashes \\. But when printing the string you will get a single slash:

>>> print(pass__)
HSSSTS00008\4Tech

The reason for this, is that a single backslash is used as an escape character to allow representation of e.g. non-printable characters as ´'\n'` (new line).

It is also possible to use the escape sequence to generate any unicode character, which is just a single backslash followed by the number describing the code point. Thus '\4' will be interpreted as code point 4. When showing the representation it is done in hex, which means that it will be represented as '\x04'.

JohanL
  • 6,671
  • 1
  • 12
  • 26