所以我现在正在学习python,并且正在创建一个函数.使用'和'之间有什么区别.我将在下面创建一个示例函数来举例说明我的问题.
def question(variable):
print variable
Run Code Online (Sandbox Code Playgroud)
现在打电话有什么区别
question("hello")
Run Code Online (Sandbox Code Playgroud)
和
question('hello')
Run Code Online (Sandbox Code Playgroud)
他们都打印你好,但为什么我可以同时使用?是因为python是灵活的吗?我很困惑,因为'通常用于字符,因为"是用于java的字符串吗?
use*_*312 41
两者都是平等的,你使用的完全是你的偏好.
就charvs string事物而言,参考Zen of Python,(PEP 20或import this)
Special cases aren't special enough to break the rules.
Run Code Online (Sandbox Code Playgroud)
长度为1的字符串不够特殊,无法使用专用char类型.
请注意,您可以这样做:
>>> print 'Double" quote inside single'
Double" quote inside single
>>> print "Single' quote inside double"
Single' quote inside double
Run Code Online (Sandbox Code Playgroud)
Python 没有字符单引号和字符串双引号的限制。
正如您在这里看到的,语法明确允许字符串两者。
http://docs.python.org/reference/lexical_analysis.html#string-literals