如何从字符串文字构建字符串对象?
How can a string object be built from a string literal?
看到这个。
A string is a Python object representing a text value. It can be built from a string literal, or it could be read from a file, or it could originate from many other sources.
我真的没看懂,怎么能从一个字符串字面量中构建出一个字符串对象呢?
'''
multiline
content
'''
另外,上面的字符串字面量是怎么来的?请帮助我理解字符串文字和字符串对象之间的区别?
请参阅 Literals 上的 python 文档:
Literals are notations for constant values of some built-in types.
字符串文字是存在于源代码中的东西 - 它是一种写入字符串值的方式。字符串对象是 str
对象在内存中的表示。您可以将其与整数文字进行比较,例如 5
:
x = 5 # x is assigned an integer value, using the literal `5`
y = x + x # x is assigned a value
# Both are `int` objects
print(isinstance(x, int)) # True
print(isinstance(x, int)) # True
foo = "hello" # foo is assigned a str value, using the literal `"hello"`
bar = foo.upper() # bar is assigned a `str` value
# Both are `str` objects
print(isinstance(foo, str)) # True
print(isinstance(bar, str)) # True
如果需要,您可以做一些 further reading:
In computer science, a literal is a notation for representing a fixed value in source code.
...
Literals are often used to initialize variables, ...
看到这个
A string is a Python object representing a text value. It can be built from a string literal, or it could be read from a file, or it could originate from many other sources.
我真的没看懂,怎么能从一个字符串字面量中构建出一个字符串对象呢?
'''
multiline
content
'''
另外,上面的字符串字面量是怎么来的?请帮助我理解字符串文字和字符串对象之间的区别?
请参阅 Literals 上的 python 文档:
Literals are notations for constant values of some built-in types.
字符串文字是存在于源代码中的东西 - 它是一种写入字符串值的方式。字符串对象是 str
对象在内存中的表示。您可以将其与整数文字进行比较,例如 5
:
x = 5 # x is assigned an integer value, using the literal `5`
y = x + x # x is assigned a value
# Both are `int` objects
print(isinstance(x, int)) # True
print(isinstance(x, int)) # True
foo = "hello" # foo is assigned a str value, using the literal `"hello"`
bar = foo.upper() # bar is assigned a `str` value
# Both are `str` objects
print(isinstance(foo, str)) # True
print(isinstance(bar, str)) # True
如果需要,您可以做一些 further reading:
In computer science, a literal is a notation for representing a fixed value in source code. ... Literals are often used to initialize variables, ...