在 Python 中剥离 [英] Strip in Python

查看:25
本文介绍了在 Python 中剥离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于 Python 中 strip() 的问题.我正在尝试从字符串中去除分号,当分号位于字符串末尾时,我知道如何执行此操作,但是如果它不是最后一个元素,我该怎么做,而是说第二个元素到最后一个元素.

例如:

1;2;3;4;\n

我想去掉最后一个分号.

解决方案

也去掉其他字符.

<预><代码>>>>'1;2;3;4;\n'.strip('\n;')'1;2;3;4'

I have a question regarding strip() in Python. I am trying to strip a semi-colon from a string, I know how to do this when the semi-colon is at the end of the string, but how would I do it if it is not the last element, but say the second to last element.

eg:

1;2;3;4;\n

I would like to strip that last semi-colon.

解决方案

Strip the other characters as well.

>>> '1;2;3;4;\n'.strip('\n;')
'1;2;3;4'

这篇关于在 Python 中剥离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆