Properly format a string

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.

3 answers

  • answered 2018-11-08 07:37 Vineeth Sai

    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'
    

  • answered 2018-11-08 07:39 Adam Smith

    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", ...
    

  • answered 2018-11-08 09:17 JohanL

    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'.