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! 😉
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! 🤯
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.