>>> s = '23edfd56ab90'
>>> ':'.join([ i+j for i,j in zip(s[::2],s[1::2])])
'23:ed:fd:56:ab:90'
Alt said:Code:string = "23edfd56ab90" re.sub(r'(.{2})(?!$)', r'\1:', string)
re.sub(r'(..)', r'\1:', string, 5)
He is "writing a script to convert MAC addresses"expl said:This is bad. Regular expressions should be avoided when simple algorithm can be used, they are waste of resources.
Show us your code.hi,
i want to do the same but i have problem with space for example
'23edfd56ab90' to '23:ed:fd:56:ab:90:'
'23 ed f d5 6a b9 0 ' to ' 23: ed: f: d5: 6a: b9: 0: '
any help for this?
def convert(str):
# remove whitespace from string
str = str.replace(" ", "")
# split the string every 2 characters
n = 2
# create a list of every 2 characters in the str.
result = [str[i:i+n] for i in range(0, len(str), n)]
# convert the list to a colon-seperated string
string = ":".join(result)
# return the result
return string