Python Forum
[Variable as a String]>EscaSpecial characters printed - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Variable as a String]>EscaSpecial characters printed (/thread-2605.html)



[Variable as a String]>EscaSpecial characters printed - CSA75 - Mar-28-2017

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


RE: [Variable as a String]>EscaSpecial characters printed - wavic - Mar-28-2017

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/lexical_analysis.html#string-and-bytes-literals


RE: [Variable as a String]>EscaSpecial characters printed - buran - Mar-28-2017

it's because '\ooo' will give the char with octal value ooo

https://docs.python.org/2/reference/lexical_analysis.html#string-literals


RE: [Variable as a String]>EscaSpecial characters printed - wavic - Mar-28-2017

Right! Why I missed it...  Big Grin


RE: [Variable as a String]>EscaSpecial characters printed - snippsat - Mar-28-2017

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