I transport data from command line to python. And I want to convert from string (command line) to tuple (python). But I have problem with \ charactor.
In command line, I using:
C:\>python music.py -a variable="?=="
In python:
#convert variable to array
variable_array = variable.split("==")
#convert to tuple
variable_tuple = tuple(variable_array)
I get variable_tuple = (“?”,””)
The result what I need is variable_tuple = (“\?”,””)
When using
C:\>python music.py -a variable="\?=="
The result is variable_tuple = (“\\?”,””)
How can I transport data from command line to get tuple (“\?”,””) in python? I need backslash for “?”
'\\?'is a string with one backslash character and a question mark.Using
listis a convenient trick for spliting a string into characters. For example:shows
'\\?'is composed of 2 characters, not 3. And if you print it:you see it prints as just one backslash character. The double backslash,
'\\', is an escape sequence.Note also that when you print a tuple, you get the repr of its contents:
'\?'is the exact same string as'\\?'in Python. They are simply different ways of representing the same string: