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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.