【发布时间】:2019-08-22 15:22:53
【问题描述】:
我需要知道如何使用 Python 3 删除字符串中某个字符之后的所有字符。
例如对于字符串abcd (Read the tnc below!),我只需要abcd。我想删除我们在() 中的所有内容。
我现在可以使用这个 Python 代码:
mystr = "abcd (Read the tnc below!)"
char = ""
for c in mystr:
if c != "(":
char += c
else:
break
但在我看来,执行如此简单的任务的代码又长又糟糕。我也尝试在网上搜索,但没有找到任何帮助。 Python 3 是否有一些很棒的正则表达式?
谢谢!
【问题讨论】:
标签: python regex python-3.x string replace