Python – Kirk Byers Course Week 1 Part 2
In the second part of assignments for the first week of Kirk Byers Python for Network Engineers class we will be working with IPv6 addresses.
We start with the following IPv6 address: FE80:0000:0000:0000:0101:A3EF:EE1E:1719.
The goal is then to split this address into individual parts. The delimiter in an IPv6 address is a colon. For an IPv4 address we would have used a dot instead. Python has a built-in function for splitting strings. To split the address we use this function and tell Python that a colon is our delimiter.
print("IPv6 address split:") print(ipv6_split) print(type(ipv6_split))
This means that we have turned our string into a list, consisting of eight parts of the IPv6 address.
daniel@daniel-iperf3:~/python/Week1$ python3 ipv6.py IPv6 address split: ['FE80', '0000', '0000', '0000', '0101', 'A3EF', 'EE1E', '1719'] <class 'list'>
To rejoin the address again the built-in function “join()” will be used. The syntax for this function is a bit awkward besides that it’s easy to use.
ipv6_new = ":".join(ipv6_split) print("IPv6 address rejoined:") print(ipv6_new) print(type(ipv6_new))
First we tell Python to put a colon between all the parts we are joining. The output then looks like this:
daniel@daniel-iperf3:~/python/Week1$ python3 ipv6.py IPv6 address rejoined: FE80:0000:0000:0000:0101:A3EF:EE1E:1719 <class 'str'>
Note that the Continue reading