如何删除python中特定字符后的所有字符?(How to remove all characters after a specific character in python?)

编程入门 行业动态 更新时间:2024-10-18 20:22:52
如何删除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.

更多推荐

本文发布于:2023-07-23 02:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226492.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   remove   python   character   specific

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!