我有个问题。我有这样一根线: “你能把灯关了吗?”。现在我想把这句话分成这样:
['can', 'you', 'please', 'turn', 'off', 'the', 'lights?']
但是现在我还想删除末尾的?
。我知道我可以直接使用substr
,但是表达式并不总是可用的。我怎样才能发现它们,如果它们存在的话,从句子中删除它们
下面是我的代码:
given_command = "Can you please turn off the lights?"
data = given_command.lower().split(" ")
print(data)
试试
replace
您可以使用^{} (regex)模块:
输出:
如果只有一个符号要删除(} :
?
),请使用^{如果您有更多的符号,请使用^{} (我使用的是符号
?
、!
、,
和.
):相关问题 更多 >
编程相关推荐