I want to extract the ip address and erase only 555 parts when 123.456.789.555 comes out, but 555 can be two or three digits, and it's not a fixed number, so I don't know what to do with it
python
ip= "123.456.789.555"
print('.'.join(ip.split('.')[:-1])) # 123.456.789
Separate the letter with . and subtract the last part.
ip="123.456.789.555"
Extracted ip=""
if len(ip) == 15:
extracted ip + = ip[:-3]
elif len(ip) ==14:
extracted ip +=ip[:-2]
extracted ip
Find .
from the back to find out its location, and cut the string to that location. An exception occurs in rindex
when the string does not contain .
.
ip = '1.1.1.1'
Last Point Location = ip.rindex('.')
ip3 = ip[:Last Point Location]
print(ip3, ip)
© 2024 OneMinuteCode. All rights reserved.