0


I'm currently trying to debug a problem where Python 3 keeps escaping backslashes in a variable.

Situation
I have a Kubernetes Secret configured and I can see it as env variable called SECRET inside my debian-based container using env | grep SECRET. The Secret contains a Password that consists of alphabetical characters and multiple single backslashes *e.g. "h\ell\o". I now want to use that secret in my python code. I want to read it from an env variable so I don't have to write it in my code in plain text.
I use secret=os.getenv("SECRET") to reference the env variable and initialize a variable containing the secret. Using the python interactive shell calling secret directly shows, that it contains "h\\ell\\o" because Python is automatically escaping the backslashes. Calling print(secret) returns "h\ell\o" as print is interpreting the double backslashes as escaped backslashes.
I now cannot use the variable SECRET to insert the password, since it always inserts it containing double backslashes, which is the wrong password.
Image showing the described situation

Question
Is there a way to disable auto escaping, or to replace the escaped backslashes? I tried several methods using codecs.encode() or codecs.decode(). I also tried using string.replace()
I cannot change the password.

rathberm
  • 9
  • 1
  • 1
    "Using the python interactive shell calling secret directly shows, that it contains "h\\ell\\o" because Python is automatically escaping the backslashes." No, it does not. It shows that it contains `h\ell\o`, because that is how `h\ell\o` is *represented as a string literal*. "Calling print(secret) returns "h\ell\o"" No, it does not. It returns `None`. It *displays* `h\ell\o`, because *that is what the string actually contains*. "I now cannot use the variable SECRET to insert the password" Yes, you can. "since it always inserts it containing double backslashes" No, it does not. – Karl Knechtel Jan 10 '22 at 09:49
  • Welcome to Stack Overflow. I am voting to close this question because *there is not an actual problem being described here*. The output you see is correct, and the string is what you expect it to be. The issue is a misconception about what should be expected; or rather, a confusion between *representations of* things and the things themselves. Notice how you are worried about the backslashes getting doubled up, but *not* about the surrounding quotes appearing out of nowhere? The backslash doubling is the same as the quoting. It is not part of the actual string. – Karl Knechtel Jan 10 '22 at 09:50
  • Just like how if you display a list and see `[1, 2, 3]`, none of the elements of that list are commas, or square brackets. – Karl Knechtel Jan 10 '22 at 09:51
  • The length of "h\ell\o" is 7 and the length of the variable "secret" is also 7. They contain the same string. – S_Bersier Jan 10 '22 at 09:56

1 Answers1

0

You can use repr() to get the exact representation of your string.

An example for your use-case may look something like this:

>>> secret = "h\\ell\\o"
>>> print(secret)
h\ell\o
>>> print(repr(secret))
'h\\ell\\o'
>>> fixed_secret = repr(secret).replace("'", "") # Remove added ' ' before and after your secret since ' ' only represent the string's quotes
>>> print(fixed_secret)
h\\ell\\o
>>>
>>> # Just to make sure that both, secret and fixed_secret, are of type string
>>> type(secret)
<class 'str'>
>>> type(fixed_secret)
<class 'str'>
>>>
neisor
  • 384
  • 4
  • 15