Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โปรแกรม Python เพื่อลบ 0 นำหน้าออกจากที่อยู่ IP


ที่อยู่ IP ระบุไว้ด้านล่าง งานของเราคือการลบเลขศูนย์นำหน้าออกจากที่อยู่ IP ก่อนอื่นเราแยกสตริงที่กำหนดโดย “.” แล้วแปลงเป็นจำนวนเต็มและลบเลขศูนย์นำหน้าแล้วรวมกลับเป็นสตริง

ตัวอย่าง

Input : 200.040.009.400
Output : 200.40.9.400

อัลกอริทึม

Step 1: Input the IP address.
Step 2. Splits the ip by ".".
Step 3: Then convert the string to an integer we can use int (parameter) function.
Step 4: Removes the leading zeroes.
Step 5: Then convert it back to string by str (parameter) and then join them back by using join function.

โค้ดตัวอย่าง

# Python program to remove leading zeros an IP address and print #the IP
# function to remove leading zeros
def IP(ip):
   zeroip = ".".join([str(int(i)) for i in ip.split(".")])
   return zeroip ;
   # Driver code
   ip =input("Enter the IP address ::>")
print("New Ip Address ::>",IP(ip))

ผลลัพธ์

Enter the IP address ::>200.006.020.900
New Ip Address ::> 200.6.20.900