How to Join List of Unicode Strings in Python? ‘🀯’

To join a list of Unicode strings in Python, use the string.join(list) method on the delimiter string. For example, you can call 'πŸ¦‰'.join(['⭐', 'πŸ˜„', '🐍']) to obtain the string 'β­πŸ¦‰πŸ˜„πŸ¦‰πŸ'. Note that per default, all strings are UTF-8 in Python which means they already are Unicode encoded.

This morning—when reading a WhatsApp message during my coffee break β˜•— I wondered: what about the Python unicode support? Can you process special Unicode characters such as ⭐ and 🐍 within normal strings? As it turns out, you can! But not with all tools (many of them are still legacy). Read on to learn more about the state of Unicode support in Python! πŸ˜‰

Problem: Given a Python list of unicode strings. How to join all list element to a single string?

Example: Say, you have the following unicode Python list:

a = [u'⭐', u'πŸ˜„', u'🐍', u'πŸ±β€πŸ', u'πŸ€—', u'❀']

Note that in Python 3+, all strings are already UTF-8 encoded—which means that they are Unicode per default! That’s why the following statement without the 'u' prefix does the same thing as the previous statement:

a = ['⭐', 'πŸ˜„', '🐍', 'πŸ±β€πŸ', 'πŸ€—', '❀']

Your goal is to join the elements with a delimiter, say ' πŸ‘» ' to generate the following output:

⭐   πŸ‘»   πŸ˜„   πŸ‘»   🐍   πŸ‘»   πŸ±β€πŸ   πŸ‘»   πŸ€—   πŸ‘»   ❀

Solution: The method is theoretically simple—you use the string.join(list) method to join the list of unicode strings.

a = ['⭐', 'πŸ˜„', '🐍', 'πŸ±β€πŸ', 'πŸ€—', '❀']
print('   πŸ‘»   '.join(a))

This generates the output:

⭐   πŸ‘»   πŸ˜„   πŸ‘»   🐍   πŸ‘»   πŸ±β€πŸ   πŸ‘»   πŸ€—   πŸ‘»   ❀

Unfortunately, many Python editors cannot handle it (they still live in the 20th century). My IDLE code editor simply crashes if I try to copy and paste the code into it! 🀯

The online code editor Trinket.io complains (this is a screenshot):

However, the Repl.it code editor works! Try it yourself in your browser:

Exercise: Click “Run” and see the Unicode output—isn’t this beautiful? Try other Unicode characters (hit WindowsSymbol + . to open the Emoji table).

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skillsβ€”even if you are a complete beginner.

Join my free webinar β€œHow to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, tooβ€”from the comfort of your own home.

Join the free webinar now!