AI & Python #7: Stop Overusing “+” to Join Strings in Python
Here are 3 alternatives that will help you do more than joining strings.
One common task data analysts have to deal with when collecting and cleaning data is working with strings. This involves formatting as well as joining strings (also known as string concatenation).
Joining strings in Python is as simple as using the plus operator +
. You’ve probably used the code below hundreds of times to join strings in Python.
>>> str1 = "Hello "
>>> str2 = "World"
>>> str1 + str2
"Hello World"
This approach is fine. However, this becomes impractical when working with lists, dataframes, or if you’re aiming for readability.
In this article, I‘ll show you 3 alternatives that will not only help you concatenate strings but also allow you to easily join list elements, properly format strings in Python, and make debugging less complicated.
Keep reading with a 7-day free trial
Subscribe to Artificial Corner to keep reading this post and get 7 days of free access to the full post archives.