Python, a smarter way of string to integer conversion
Posted
by Hellnar
on Stack Overflow
See other posts from Stack Overflow
or by Hellnar
Published on 2010-03-23T12:53:27Z
Indexed on
2010/03/23
13:03 UTC
Read the original article
Hit count: 439
Hello I have written this code to convert string in such format "0(532) 222 22 22" to integer such as 05322222222 .
class Phone():
def __init__(self,input):
self.phone = input
def __str__(self):
return self.phone
#convert to integer.
def to_int(self):
return int((self.phone).replace(" ","").replace("(","").replace(")",""))
test = Phone("0(532) 222 22 22")
print test.to_int()
It feels very clumsy to use 3 replace methods to solve this. I am curious if there is a better solution?
© Stack Overflow or respective owner