Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not print "\05" or "\07" etc?

When I try to run:

print "\05"

Nothing is printed. If I change the 5 after the \0 to another number something weird will often happen. Why is this?

like image 963
Roger ALex Avatar asked Feb 08 '23 04:02

Roger ALex


2 Answers

Back slash is an escape sequence to add literal characters to a string by ordinal value. This means you take an ASCII character code and literally insert it into your string. The reason you would do this is that some characters are not valid in source code.

In your case you entered \05 which will be the [enquiry] character which is not printable and rarely used. Thus python will have printed it but nothing will have happened.

For your reference I include a table of character codes from the wikimedia commons, you can use this to find out what each of the character codes will do.

table of ascii character codes (File under public domain from Wikimedia commons, can be found here).

However if in the other hand you want to print the literal string \05 you need to escape the bashslash (backslash escapes the next character or characters to cause them to have secondary meanings like inserting by character code as shown above). In your case we need to escape the backslash which changes it from being an escape character into a normal one, we do this with a second bashslash (the first to escape the second). So ultimately your code to do what I think you wanted will be:

print "\\05"

Which will output a literal \05

like image 98
Vality Avatar answered Feb 12 '23 11:02

Vality


For ASCII, only characters in the range 0x20 to 0x7E are printable characters.

The other ASCII characters, have no standard display. For example, the character 0x07, a.k.a, BEL, usually generates a beep, but it also might be a visual alarm or something else.

like image 35
Yu Hao Avatar answered Feb 12 '23 11:02

Yu Hao