In Python, strings are immutable, so you have to create a new string.
(在Python中,字符串是不可变的,因此您必须创建一个新字符串。)
You have a few options of how to create the new string. (您有一些有关如何创建新字符串的选项。)
If you want to remove the 'M' wherever it appears: (如果要删除出现的“ M”,请执行以下操作:)
newstr = oldstr.replace("M", "")
If you want to remove the central character:
(如果要删除中心字符:)
midlen = len(oldstr)/2 # //2 in python 3
newstr = oldstr[:midlen] + oldstr[midlen+1:]
You asked if strings end with a special character.
(您询问字符串是否以特殊字符结尾。)
No, you are thinking like a C programmer. (不,您在想像C程序员。)
In Python, strings are stored with their length, so any byte value, including \0
, can appear in a string. (在Python中,字符串按长度存储,因此任何字节值(包括\0
)都可以出现在字符串中。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…