0

The following python code has some weird behavior which I don't understand

print('\x1bZ')

when I run this code whether in a file or in the interpreter I have a wierd outcome: terminal result

actual values as displayed when you write this value to a file as bytes: bytes in text file

Discoveries at time of posting this question:

  1. whether single quotes or double quotes make a difference (they don't)
  2. 0x1b is hex for 27 which in ascii is ESC which matches as displayed with the second picture. This lead me to theorize that the letter Z in the string literal can be replaced but as per my test in point number 3 it cant be reproduced with other letters
  3. instead of \x1bZ (ESC and then Z) trying ESC and then some other letter (I haven't checked all possibilities) yielded no apparent result except from replacing Z with c which seems to clear the terminal

Hypothesis that I came up with:

Questions:

  • Why is this particular sequence of characters results in this output?
  • What is VT100 and how it is related if it is related? (I visited it's Wikipedia page)
  • whether it is possible to print a string that contains this specific sequence without that weird outcome as displayed in the first picture

all help and knowledge about this will be appreciated!!

  • `\x1b` is the "ESC" for a system: https://stackoverflow.com/questions/15011478/ansi-questions-x1b25h-and-x1be You can ensure those characters print using an backslash escape: `print('\\x1b')` https://stackoverflow.com/questions/19095796/how-can-i-print-a-single-backslash – Glycerine Oct 14 '22 at 20:55
  • @Glycerine I was already acquainted with the double backslash but its not quite what I'm looking for. I would like to output the actual value rather than the hex string representation of this value, and moreover: I would like to do so without the weird output. Thanks for the informing links! – danienachumdev Oct 14 '22 at 21:26
  • Why would you want to print an unprintable character (ESC)? Sequences beginning with ESC are instructions to the terminal(emulator); ESC Z means: dear terminal, please send back another ESC-sequnece that contains informations about your features. – Lorinczy Zsigmond Oct 16 '22 at 04:25

0 Answers0