Request Help With Byte/String Problem

Grant Edwards grant.b.edwards at
Fri Dec 2 10:11:18 EST 2016

On 2016-12-02, Wildman via Python-list <python-list at> wrote:
> On Wed, 30 Nov 2016 14:39:02 +0200, Anssi Saari wrote:
>> There'll be a couple more issues with the printing but they should be
>> easy enough.
> I finally figured it out, I think.  I'm not sure if my changes are
> what you had in mind but it is working.  Below is the updated code.
> Thank you for not giving me the answer.  I was a good learning
> experience for me and that was my purpose in the first place.
> def format_ip(addr):
>     return str(int(addr[0])) + '.' + \ # replace ord() with int()
>            str(int(addr[1])) + '.' + \
>            str(int(addr[2])) + '.' + \
>            str(int(addr[3]))

This is a little more "pythonic":

    def format_ip(addr):
        return '.'.join(str(int(a) for a in addr))

I don't know what the "addr" array contains, but if addr is a byte
string, then the "int()" call is not needed, in Pythong 3, a byte is
already an integer:

    def format_ip(a):
       return '.'.join(str(b) for b in a)

addr = b'\x12\x34\x56\x78'


If <addr> is an array of strings containing base-10 representations of
integers, then the str() call isn't needed either:

def format_ip(a):
    return '.'.join(s for s in a)

addr = ['12','34','56','78']


Grant Edwards               grant.b.edwards        Yow! TAILFINS!! ... click
                                  at               ...

More information about the Python-list mailing list