Python unicode list join

15,457
>>> a = [u'00', u'0c', u'29', u'58', u'86', u'16']
>>> u":".join(a)
u'00:0c:29:58:86:16'
>>> str(u":".join(a))
'00:0c:29:58:86:16'
Share:
15,457

Related videos on Youtube

wagner-felix
Author by

wagner-felix

Updated on June 04, 2022

Comments

  • wagner-felix
    wagner-felix almost 2 years

    I want to join a unicode python list, for example:

    a = [u'00', u'0c', u'29', u'58', u'86', u'16']
    

    I want a string that looks like this:

    '00:0c:29:58:86:16'
    

    How would I join this?

  • agf
    agf over 12 years
    Or the other way: b':'.join(str(item) for item in a)
  • Rafael S. Calsaverini
    Rafael S. Calsaverini over 11 years
    I tried this and I keep getting a UnicodeDecodeError about unicode characters with an accent. Does anyone knows why?
  • MattH
    MattH over 11 years
    @RafaelS.Calsaverini: If your error is on the str part of this solution it is because the character cannot be represented in ascii. If you need your unicode string in ascii you will have to make an approximation, try a solution like this one: stackoverflow.com/a/8087466/267781