0

So I made a ipv4 breakdown program using python and needed to print output in a formatted way but there's a problem.

IPv4 Break Down Program

Please enter an IPv4 Address and prefix (#.#.#.#/Prefix): 27.34.31.254/29

Address:        27.34.31.254    00011011.00100010.00011111.11111110
Netmask:        255.255.255.248 11111111.11111111.11111111.11111000
Wildcard:       0.0.0.7 00000000.00000000.00000000.00000111
----------------------------------------------------------------------
Network:        27.34.31.248    00011011.00100010.00011111.11111000
Broadcast:      27.34.31.255    00011011.00100010.00011111.11111111
HostMin:        27.34.31.249    00011011.00100010.00011111.11111001
HostMax:        27.34.31.254    00011011.00100010.00011111.11111110
Host/Net:       6

Nischal   >   python SA_IPv4BreakDown.py
IPv4 Break Down Program

Please enter an IPv4 Address and prefix (#.#.#.#/Prefix): 192.168.2.142/20

Address:        192.168.2.142   11000000.10101000.00000010.10001110
Netmask:        255.255.240.0   11111111.11111111.11110000.00000000
Wildcard:       0.0.15.255      00000000.00000000.00001111.11111111
----------------------------------------------------------------------
Network:        192.168.0.0     11000000.10101000.00000000.00000000
Broadcast:      192.168.15.255  11000000.10101000.00001111.11111111
HostMin:        192.168.0.1     11000000.10101000.00000000.00000001
HostMax:        192.168.15.254  11000000.10101000.00001111.11111110
Host/Net:       4094

See when the wildcard has many numbers it prints in right order where as while the wildcard has low numbers it doesn't. How can I solve this? BTW I wrote this to print:

print("")
print("Address:" + "\t" + parts[0] + "\t" + binary_ip)
print("Netmask:" + "\t" + subnet_mask + "\t" + subnet_mask_binary)
print("Wildcard:" + "\t" + wildcard_ + "\t" + binary_wildcard)
print("----------------------------------------------------------------------")
print("Network: " + "\t" + network_decimal + "\t" + network_binary)
print("Broadcast: " + "\t" + broadcast + "\t" + broadcast_binary)
print("HostMin: " + "\t" + hostmin_decimal + "\t" + hostmin_binary)
print("HostMax: " + "\t" + hostmax_decimal + "\t" + hostmax_binary)
print("Host/Net: " + "\t" + str(hosts))
  • 1
    Just like in Word. Simply hitting the Tab key will not create a table. Reserve a fixed amount of space for the IP address. – Thomas Weller Jun 20 '22 at 16:53
  • Use a formatting function or f-string, they allow you to specify field widths. – Barmar Jun 20 '22 at 16:54
  • Related (maybe a better example): https://stackoverflow.com/questions/10837017/how-do-i-make-a-fixed-size-formatted-string-in-python – Thomas Weller Jun 20 '22 at 16:55

0 Answers0