简体   繁体   English

Python命令行“字符”返回“字符”

[英]Python Command Line “characters” returns 'characters'

Thanks in advance for your help. 在此先感谢您的帮助。

When entering "example" at the command line, Python returns 'example'. 在命令行中输入“ example”时,Python返回“ example”。 I can not find anything on the web to explain this. 我在网上找不到任何可以解释的内容。 All reference materials speaks to strings in the context of the print command, and I get all of the material about using double quotes, singles quotes, triple quotes, escape commands, etc. 所有参考资料都在print命令的上下文中谈到字符串,而我获得了有关使用双引号,单引号,三引号,转义命令等的所有资料。

I can not, however, find anything explaining why entering text surrounded by double quotes at the command line always returns the same text surrounded by single quotes. 但是,我找不到任何能解释为什么在命令行中输入用双引号引起来的文本总是返回相同的用单引号引起来的文本的解释。 What gives? 是什么赋予了? Thanks. 谢谢。

In Python both 'string' and "string" are used to represent string literals . 在Python中, 'string'"string"都用于表示字符串文字 It's not like Java where single and double quotes represent different data types to the compiler. 它与Java不同,单引号和双引号代表了编译器不同的数据类型。

The interpreter evaluates each line you enter and displays this value to you. 解释器评估您输入的每一行,并向您显示此值。 In both cases the interpreter is evaluating what you enter, getting a string, and displaying this value. 在这两种情况下,解释器都会评估您输入的内容,获取字符串并显示此值。 The default way of displaying strings is in single quotes so both times the string is displayed enclosed in single quotes. 显示字符串的默认方式是用单引号引起来,因此两次显示字符串时都用单引号引起来。

It does seem odd - in that it breaks Python's rule of There should be one - and preferably only one - obvious way to do it - but I think disallowing one of the options would have been worse. 看起来确实很奇怪-因为它打破了Python的规则, 应该有一种-最好只有一种-显而易见的方式来做到这一点 -但我认为不允许其中一种选择会更糟。

You can also enter a string literal using triple quotes: 您还可以使用三引号输入字符串文字:

>>> """characters
... and
... newlines"""
'characters\nand\nnewlines'

You can use the command line to confirm that these are the same thing: 您可以使用命令行来确认它们是否相同:

>>> type("characters")
<type 'str'>
>>> type('characters')
<type 'str'>
>>> "characters" == 'characters'
True

The interpreter uses the __repr__ method of an object to get the display to print to you. 解释器使用对象的__repr__方法将显示内容打印给您。 So on your own objects you can determine how they are displayed in the interpreter. 因此,您可以在自己的对象上确定它们在解释器中的显示方式。 We can't change the __repr__ method for built in types, but we can customise the interpreter output using sys.displayhook : 我们无法更改内置类型的__repr__方法,但可以使用sys.displayhook自定义解释器输出:

>>> import sys
>>> def customoutput(value):
...     if isinstance(value,str):
...        print '"%s"' % value
...     else:
...        sys.__displayhook__(value)
...
>>> sys.displayhook = customoutput
>>> 'string'
"string"

In python, single quotes and double quotes are semantically the same. 在python中,单引号和双引号在语义上是相同的。 It struck me as strange at first, since in C++ and other strongly-typed languages single quotes are a char and doubles are a string . 起初,我感到很奇怪,因为在C ++和其他强类型语言中,单引号是char而双引号是string Just get used to the fact that python doesn't care about types, and so there's no special syntax for marking a string vs. a character. 只是习惯了python并不关心类型的事实,因此没有用于标记字符串还是字符的特殊语法。 Don't let it cloud your perception of a great language 不要让它模糊您对一门好语言的看法

Don't get confused. 不要感到困惑。 In python single quotes and double quotes are same. 在python中,单引号和双引号相同。 The creates an string object. 创建一个字符串对象。

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

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