简体   繁体   English

为什么我不能打印“\\ 05”或“\\ 07”等?

[英]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. 如果我将\\0之后的5更改为另一个数字,通常会发生奇怪的事情。 Why is this? 为什么是这样?

For ASCII, only characters in the range 0x20 to 0x7E are printable characters . 对于ASCII,只有0x200x7E范围内的字符可打印字符

The other ASCII characters, have no standard display. 其他ASCII字符没有标准显示。 For example, the character 0x07 , aka, BEL , usually generates a beep, but it also might be a visual alarm or something else. 例如,字符0x07 ,aka, BEL ,通常会发出蜂鸣声,但它也可能是视觉警报或其他内容。

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. 这意味着您将ASCII字符代码插入字符串中。 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. 在您的情况下,您输入\\05这将是[enquiry]字符,该字符不可打印且很少使用。 Thus python will have printed it but nothing will have happened. 因此python将打印它,但什么都不会发生。

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. 作为参考,我提供了一个维基媒体公共字符代码表,你可以用它来找出每个字符代码的作用。

ascii字符代码表 (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). 但是,如果另一方面你想要打印文字字符串\\05你需要转义bashslash(反斜杠转义下一个或多个字符,使它们具有二次含义,如上面显示的字符代码插入)。 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). 在你的情况下,我们需要转义反斜杠,将其从一个转义字符转换为普通字符,我们使用第二个bashslash(第一个转义第二个)。 So ultimately your code to do what I think you wanted will be: 所以最终你的代码做我认为你想要的将是:

print "\\05"

Which will output a literal \\05 哪个会输出文字\\05

It's not a valid (printable) ASCII character. 它不是有效(可打印)的ASCII字符。 Do you mean that you literally want to print "\\05"? 你的意思是你真的想打印“\\ 05”吗? If so, just escape the backslash with another backslash, like so: 如果是这样,只需使用另一个反斜杠转义反斜杠,如下所示:

print "\\05"

Try this. 尝试这个。 You need the extra "\\" to escape. 你需要额外的“\\”才能逃脱。

 print "\\05"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM