我正在从字符串中删除一个字符,如下所示:
S = "abcd"
Index=1 #index of string to remove
ListS = list(S)
ListS.pop(Index)
S = "".join(ListS)
print S
#"acd"
我相信这是最好的方法.
EDIT
谁能帮帮我吗.哪种Python 式的方法?
我正在从字符串中删除一个字符,如下所示:
S = "abcd"
Index=1 #index of string to remove
ListS = list(S)
ListS.pop(Index)
S = "".join(ListS)
print S
#"acd"
我相信这是最好的方法.
EDIT
谁能帮帮我吗.哪种Python 式的方法?
您可以通过切片绕过所有列表操作:
S = S[:1] + S[2:]
或者更一般地说
S = S[:Index] + S[Index + 1:]
你的问题的许多答案(包括像这样的答案)可以在这里找到:How to delete a character from a string using python?.然而,这个问题名义上是关于按值删除,而不是按索引删除.