简体   繁体   English

如何判断具有空字符串值的 python 字符串变量在初始化时是使用单引号还是双引号?

[英]How can i tell if a python string variable with an empty string value is using single quotes or double quotes at init time?

Is there any way to tell how an empty string variable was intialized in python?有什么方法可以告诉 python 中的空字符串变量是如何初始化的? If I initialize a variable with empty double-quotes.如果我用空的双引号初始化一个变量。 Can I later find out that variable was initialized with empty double quotes vs empty single quotes?我以后可以发现该变量是用空双引号还是空单引号初始化的吗?

>>> test_str = ""
>>> type(test_str)
<class 'str'>
>>> test_str = ''
>>> type(test_str)
<class 'str'>

Thanks谢谢

No. The starting quote type merely changes what terminating quote type the parser will look for while parsing the source code.不,起始引号类型仅更改解析器在解析源代码时将查找的终止引号类型。 Meaning, if the string starts with ' , then the parser will look for the next ' to know when the string ends, and vice versa with " . That's all. That information is not relevant to anything else and isn't preserved. There are also a thousand other ways to create a string, eg:意思是,如果字符串以'开头,则解析器将查找下一个'以了解字符串何时结束,反之亦然" 。仅此而已。该信息与其他任何内容无关,也不会保留。有还有一千种其他方法来创建字符串,例如:

test_str = '' "" '' "" + '' + ""

Same result, an empty string.同样的结果,一个空字符串。 How would you even preserve this information and to what end?你甚至会如何保存这些信息以及为了什么目的?

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

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