如何删除python中特定字符后的所有字符? [英] How to remove all characters after a specific character in python?
本文介绍了如何删除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.
推荐答案
在分隔符上最多拆分一次,并取第一块:
Split on your separator at most once, and take the first piece:
sep = '...'
stripped = text.split(sep, 1)[0]
您没有说明如果分隔符不存在会发生什么.在这种情况下,this 和 Alex 的解决方案都将返回整个字符串.
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.
这篇关于如何删除python中特定字符后的所有字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文