Removing characters from a string - Python

1

Well, I have the following problem.

I have a function that returns me the following output:

"Address:58.200.133.200"

I would like to save this value to a ip variable, but I want only the ip part from output, so I get ip = "58.200.133.200" .

I do not have access to the function that generates this output, I just call it through an API.

    
asked by anonymous 25.09.2014 / 21:48

3 answers

2

If your string always has this format, simply get its suffix - because the prefix Address: (8 letters) is fixed:

s = "Address:58.200.133.200"
ip = s[8:]

If your entry has some variance, the use of regular expressions might be more interesting, as pointed out in the Gypsy answer

a>.

    
25.09.2014 / 22:04
1

I think it would be the case to use a regular expression :

import re
p = re.compile("Address:([\d\.]*)")
match = p.match(sua_string)
resultado = match.group()
    
25.09.2014 / 22:01
1

If you do not use regex as the colleague explained, you can do:

>>> txt = "Address:58.200.133.200"
>>> txt.split(':')
['Address', '58.200.133.200']
>>> txt.split(':')[0]
'Address'
>>> txt.split(':')[1]
'58.200.133.200'
    
02.10.2014 / 22:07