Python:按'and'拆分字符串

2024-04-29 07:05:22 发布

您现在位置:Python中文网/ 问答频道 /正文

如何通过撇号'-拆分字符串?

例如,给定string = "pete - he's a boy"


Tags: 字符串stringheboypete
3条回答

这感觉有点老套,但你可以:

string.replace("-", "'").split("'")
string = "pete - he's a boy"
result = string.replace("'", "-").split("-")
print result

['pete ', ' he', 's a boy']

可以使用正则表达式模块的拆分函数:

re.split("['-]", "pete - he's a boy")

相关问题 更多 >