Search code examples
pythonstringstrip

Strange behaviour of Python strip function


I have this code:

st = '55000.0'
st = st.strip('.0')
print st

I expected it to print 55000, but instead it prints 55. I thought perhaps the . in the argument might need to be escaped (like in a regular expression); so I also tried st = st.strip('\.0'), but the result is the same.

Why are all the zeros removed from the input? Why doesn't it stop after removing the .0?


Solution

  • You've misunderstood strip() - it removes any of the specified characters from both ends; there is no regex support here.

    You're asking it to strip both . and 0 off both ends, so it does - and gets left with 55.

    See the official String class docs for details.