如何删除python中特定字符后的所有字符?(How to remove all characters after a specific character in python?)
我有一个字符串 某个字符后如何删除所有文字? ( 在这种情况下... ) 之后的文字将...改变,所以我就是为什么我要删除一个特定的字符之后的所有字符。
I have a string. How do I remove all text after a certain character? (In this case ...) The text after will ... change so I that's why I want to remove all characters after a certain one.
最满意答案
最多一次分离到分离器上,并取第一块:
sep = '...' rest = text.split(sep, 1)[0]你没有说如果分隔符不存在会发生什么。 在这种情况下,这个和Alex的解决方案都将返回整个字符串。
Split on your separator at most once, and take the first piece:
sep = '...' rest = text.split(sep, 1)[0]You didn't say what should happen if the separator isn't present. Both this and Alex's solution will return the entire string in that case.
更多推荐
发布评论