如何从字符串文字建立一个字符串对象?

问题描述 投票:0回答:1

请看这个 回答.

字符串是一个表示文本值的 Python 对象。它可以从一个字符串文字建立,也可以从一个文件中读取,或者它可以来源于许多其他来源。

我真的不明白,字符串对象怎么能从字符串文字建立?

'''
multiline
content
'''

还有,上面的怎么是字符串 literal?请大家帮我理解一下字符串字元和字符串对象的区别?

python string object string-literals
1个回答
0
投票

请看下面的python文档 字数:

字符是一些内置类型的常量值的符号。

字符是存在于源代码中的东西--它是书写字符串值的一种方式。字符串对象是一个字符串的内存表示。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

你可以做一些 进一步阅读,如果需要的话。

在计算机科学中,文字是一种符号 在源代码中表示一个固定的值。 ... 字符经常被用来初始化变量,...

© www.soinside.com 2019 - 2024. All rights reserved.