How to concatenate multiple unicode string?

24,218

if you want to concatenate two strings use +

>>> '가' + 'ㄱ'
'\xea\xb0\x80\xe3\x84\xb1'
>>> u'가' + u'ㄱ'
u'\uac00\u3131'
>>> print u'가' + u'ㄱ'
가ㄱ

this means you can use

output1 + output2
Share:
24,218
user1732445
Author by

user1732445

Updated on November 06, 2020

Comments

  • user1732445
    user1732445 over 3 years

    I have two unicode string '가' and 'ㄱ' and I want to concatenate them to get "가ㄱ"

    This is my code:

    output1 = unicodeQueue(self.queue) # first unicode result
    output2 = unicodeQueue(self.bufferQueue) # second unicode result
    sequence = [output1, output2]
    print sequence
    output = ''.join(sequence)
    return output
    

    And this is the output I'm getting:

    [u'\uac00', u'\u3131']
    ㄱ가가ㄱ가
    

    I don't know why it doesn't produce correct result, can anyone help me with this?

    • NPE
      NPE over 11 years
      I am unable to reproduce this.
    • user1732445
      user1732445 over 11 years
      @NPE I uploaded my partial script, the main of my question is "how to concatenate two unicode in python?"
    • SilentGhost
      SilentGhost over 11 years
      @user1732445: there's nothing obviously wrong w/ your code.
    • kennytm
      kennytm over 11 years
      Works for me. May be try u''.join(sequence).
    • user1732445
      user1732445 over 11 years
      @KennyTM Hmm. Thanks.. at now It works for me...
    • EnricoGiampieri
      EnricoGiampieri over 11 years
      Same here, nothing wrong with the join, it works as supposed to the unicodeQueue class?
  • Perkins
    Perkins over 11 years
    Keep in mind that + works more slowly than unicode.join(u'',vals) or str.join('',vals), but if it works when join doesn't, by all means, use it.