如何在 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,因為它們不會被駐留。