Elegant Format for a MAC Address in Python 3.2

后端 未结 8 2193
无人及你
无人及你 2020-12-19 00:46

I am looking for a more elegant solution to formatting a MAC address with colons. I am using Python 3.2. A fancy list comprehension perhaps?

s=\"\"
h=\"0023         


        
8条回答
  •  感情败类
    2020-12-19 01:12

    Not sure how pretty this is, but it will do what you ask:

    ':'.join([h[i:i+2] for i,j in enumerate(h) if not (i%2)])
    

    gives:

    '00:23:3a:99:0c:21'
    

提交回复
热议问题