X

Join a list to a string in Python

Lists are a great data structure but they’re not always useful, for example when writing a database or if you’re building a URL from a series of values in a list, you may prefer a <class ‘str’>.

Let’s take the later instance in this example.

list1 = ['www','example','com']

string = '.'.join(list1)

Breaking it down is very simple, in calling a join function on a variable, the list (between brackets) is joined together with the delimiter specified within the quotes.

Try changing the above to a space, comma, dash to see how the results change!

Will Cecil: Digital Marketer, Python Tinkerer & Tech Enthusiast. Follow me: Website / Twitter / Github
Related Post