Variable has a backslash, when the variable is added to create a list, the backslash is duplicated, how can I avoid it?

Hello All. I am new in python and let me explain what do I need…

I have to add some variables to a program using raw_input so I have 3 variables, name_input, value_input and description_input, after type these inputs I create the following code:

      print (value_input)
      Name = ['name', name_input ]
      Value = ['value', value_input]
      Description = ['description', description_input]
      print (Value)

The problem is when the value_input has a backslash, for example

value_input = "Value \ parameter null "

The value from value_input was typed with raw_input.

The output is…:

>>> Value \ parameter null

>>> ['value', 'Value \\ parameter null']

Why a backslash is added? how can I avoid this?

I did some tests with append and the same behaviour.

The goal is get the following:

["['name', parameter 1]", ['value', 'true \ not modify'], ['description', 'any description']"]

but I got this…

["['name', parameter 1]", ['value', 'true \\ not modify'], ['description', 'any description']"]

I hoe anyone can help me!!!
Thanks in advance.

Short version: there isn’t a second backslash, it’s just the way the string is displayed.

The backslash has a special use in strings: it’s the escape character. That means it’s used to say “I’m going to use a special character right now”, like a new-line \n or a tab \t. Because it’s used for that, when someone wants a real backslash they need to escape it as well, with \\

If you print the string by itself (print(value_input)), it will not show you a second backslash.

If your goal is to print the entire list as described, but without the doubled backslash, you will need to do some string formatting and arrangement to produce the full string you need.

You said raw_input. Is that the name of the function you’re used? If it is, then it sounds like you’re using Python 2.7 or thereabouts, which is no longer supported. The latest is Python 3.11.

Hello James. I will do your recommendation, thanks a lot.