python中修改元组

>>> test1 = ("aaa","bbb","ccc","ddd","eee","fff","ggg")
>>> type(test1)
<class 'tuple'>
>>> test2 = test1[:4]
>>> test2
('aaa', 'bbb', 'ccc', 'ddd')
>>> test3 = test1[4:]
>>> test3
('eee', 'fff', 'ggg')
>>> insert = "xxx",
>>> test4 = test2 + insert + test3
>>> test1
('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg')
>>> test4
('aaa', 'bbb', 'ccc', 'ddd', 'xxx', 'eee', 'fff', 'ggg')
>>> test5 = test1[:3]
>>> test5
('aaa', 'bbb', 'ccc')
>>> test6 = test1[4:]
>>> test6
('eee', 'fff', 'ggg')
>>> displace = "yyy",
>>> test7 = test5 + displace + test6
>>> id(test1)
2326018208096
>>> id(test7)
2326018299264
>>> test1
('aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff', 'ggg')
>>> test7
('aaa', 'bbb', 'ccc', 'yyy', 'eee', 'fff', 'ggg')
原文地址:https://www.cnblogs.com/liujiaxin2018/p/14171075.html