如何在 Python 中更改不可变字符串的 ID?


在 Python 中,字符串是字符序列。它是一种数据类型,用于表示文本值,例如单词、句子甚至整个文档。Python 中的字符串用单引号 ('...') 或双引号 ("...") 括起来,可以包含字母数字字符、符号、空格等。

作为不可变对象,字符串的 ID 在 Python 中无法更改。只要一个新字符串与原始字符串具有相同的值(仅对小字符串字面量有效),这两个字符串的 ID 将保持相同。

在 Python 中,具有相同值的短字符串字面量(仅包含 ASCII 字符的字符串)会被解释器进行驻留,这意味着它们将具有相同的 ID。

但是,对于较长的字符串或包含非 ASCII 字符的字符串,即使它们具有相同的值,通常也会具有不同的 ID,因为它们不会被驻留。

以下是实现此目的的三种不同方法

使用字符串连接

示例

在这里,我们将空字符串连接到原始字符串以创建一个具有相同值的新字符串。由于我们正在与空字符串连接,因此它不会更改原始字符串的值,因此它会创建一个具有相同 ID 的新字符串。

# create a string
my_string = "Hello, world!"
# create a new string with the same value but a different ID using concatenation
new_string = my_string + ""
# check IDs of both strings

print(my_string)
print(new_string)

print(id(my_string))
print(id(new_string))

输出

Hello, world!
Hello, world! 
140045518101040
140045518101040

使用字符串连接创建新字符串

示例

在此示例中,我们通过将单词“Goodbye”与原始字符串从索引 5 开始(即“Hello”部分之后)的切片连接起来创建一个新字符串。这会创建一个值为“Goodbye, world!”的新字符串,其 ID 与原始字符串不同。

# create a string
my_string = "Hello, world!"
# create a new string with a different ID
new_string = "Goodbye" + my_string[5:]
# print the original string and the new string
print("Original string:", my_string)
print("New string:", new_string)
print(id(my_string))
print(id(new_string))

输出

Original string: Hello, world!
New string: Goodbye, world!
139898304986544
139898304917744

使用字符串 replace() 方法创建新字符串

示例

在此示例中,我们使用字符串 replace() 方法将原始字符串中的单词“Hello”替换为单词“Goodbye”来创建一个新字符串。这会创建一个值为“Goodbye, world!”的新字符串,其 ID 与原始字符串不同。

# create a string
my_string = "Hello, world!"

# create a new string with a different ID
new_string = my_string.replace("Hello", "Goodbye")
# print the original string and the new string
print("Original string:", my_string)
print("New string:", new_string)
print(id(my_string))
print(id(new_string))

输出

Original string: Hello, world!
New string: Goodbye, world!
139898304554928
139898304555120

使用字符串格式化创建新字符串

示例

在此示例中,我们使用字符串格式化将原始字符串的切片插入到新的字符串模板中来创建一个新字符串。该切片从索引 7 开始(即逗号和空格之后),因此新字符串的值为“Goodbye, world!”。这个新字符串的 ID 与原始字符串不同。

# create a string

my_string = "Hello, world!"

# create a new string with a different ID
new_string = "Goodbye, {}".format(my_string[7:])
# print the original string and the new string
print("Original string:", my_string)

print("New string:", new_string)
print(id(my_string))

print(id(new_string))

输出

Original string: Hello, world!
New string: Goodbye, world!
139898304805808
139898304806256

使用 string() 构造函数

示例

在这里,我们使用 string 构造函数创建一个具有相同值的新字符串。在 Python 中,具有相同值的短字符串字面量(仅包含 ASCII 字符的字符串)会被解释器进行驻留,这意味着它们将具有相同的 ID。

# create a string
my_string = "Hello, world!"

# create a new string with the same value using the string constructor
new_string = str(my_string)
# check IDs of both strings
print(my_string)
print(new_string)
print(id(my_string))
print(id(new_string))

输出

Hello, world!
Hello, world!
140704360063024
140704360063024

使用切片

示例

在这里,我们使用切片创建一个与原始字符串具有相同值的新字符串。不带起始或结束索引的切片将返回原始字符串的副本。

在 Python 中,具有相同值的短字符串字面量(仅包含 ASCII 字符的字符串)会被解释器进行驻留,这意味着它们将具有相同的 ID。

# create a string
my_string = "Hello, world!"
# create a new string with the same value using slicing
new_string = my_string[:]
# check IDs of both strings
print(my_string)
print(new_string)
print(id(my_string))
print(id(new_string))

输出

Hello, world!
Hello, world!
140321283842096
140321283842096

结论

总之,如果两个字符串对象具有相同的值,并且该值是短字符串字面量,则它们将具有相同的 ID。但是,对于较长的字符串或包含非 ASCII 字符的字符串,即使它们具有相同的值,通常也会具有不同的 ID,因为它们不会被驻留。

更新于: 2023年8月10日

695 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.