Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange behaviour of Python strip function [duplicate]

Possible Duplicate:
python str.strip strange behavior

I have following piece of code:

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

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??

like image 476
Aamir Rind Avatar asked Nov 28 '22 10:11

Aamir Rind


1 Answers

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.

like image 86
declension Avatar answered Dec 06 '22 11:12

declension