问题描述
可能的重复:
python str.strip 奇怪的行为
我有以下一段代码:
st = '55000.0'
st = st.strip('.0')
print st
当我执行时,它只打印 55
但我希望它打印 55000
.我认为 strip
中的 dot
导致了这种情况,因为我们通常在 Regular Expression
中 escape
它,所以我也尝试了 st = st.strip('\.0')
但仍然给出相同的结果.任何想法为什么它不只是条带化 .0
以及为什么所有零条带化??
When i execute, it print only 55
but i expect it to print 55000
. I thought that the dot
in strip
causing this as we usually escape
it in Regular Expression
so i also tried st = st.strip('\.0')
but still is giving same results. Any ideas why it is not just striping .0
and why all zeros striped??
推荐答案
您误解了 strip() - 它从两端删除任何指定字符;这里没有正则表达式支持.
You've misunderstood strip() - it removes any of the specified characters from both ends; there is no regex support here.
您要求它去除两端的 .
和 0
,所以它确实 - 并留下了 55
.
You're asking it to strip both .
and 0
off both ends, so it does - and gets left with 55
.
有关详细信息,请参阅官方 String 类文档.
See the official String class docs for details.
这篇关于Python strip 函数的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!