Python Forum

Full Version: [Variable as a String]>EscaSpecial characters printed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello

I'm facing a strange situation..after setting a variable as a string, I try to print that variable and get the string with a caracter replaced by a special caracter.. Sad  

file_version_cmd="wmic datafile where name='C:\\\Program Files (x86)\\\Citrix\\\ICA Client\\\4.1\\\wfcrun32.exe'"
print(file_version_cmd)
Output:
wmic datafile where name='C:\\Program Files (x86)\\Citrix\\ICA Client\♦.1\\wfcrun32.exe'
Thanks for your help
Seems like the '4' symbol is escaped. I can't try it here cause I have no Windows on my machine. Try to put one more backslash there.

I've reproduced it here too.
When you want to print a '\', escape it. Want to print two '\' - escape both. It's a general rule. '\' is the escape character. When it precedes another character if Python recognises the escape sequence, you get something else. I can't tell why this happens with '4'.
https://docs.python.org/3.5/reference/le...s-literals
it's because '\ooo' will give the char with octal value ooo

https://docs.python.org/2/reference/lexi...g-literals
Right! Why I missed it...  Big Grin
Just use all \(backslash) the other way(/),the no octal value or other escape characters problems.
>>> s = '\4.1'
>>> s
'\x04.1'
>>> print(s)
.1
>>> # Fix
>>> s = '/4.1'
>>> s
'/4.1'
>>> print(s)
/4.1
>>> s = 'C:/Program Files (x86)/Citrix/ICA Client/4.1/wfcrun32.exe'
>>> print(s)
C:/Program Files (x86)/Citrix/ICA Client/4.1/wfcrun32.exe