Python How to Join a List of Strings

To join a list of strings into a string in Python:

  1. Specify a separator string.
  2. Call the join() method on the separator string and the list.
  3. See the merged string as a result.

For example, let’s join a list of words by a dash:

words = ["This", "is", "a", "test"]
joined = "-".join(words)

print(joined)

Output:

This-is-a-test

Perhaps the most common question is how to join strings into one string separated by space.

To do this:

  1. Specify a blank space as the separator strings.
  2. Call the join() method on the separator and the list of strings.

Here is how it looks in code.

words = ["This", "is", "a", "test"]
joined = " ".join(words)

print(joined)

Output:

This is a test

If you were looking for a quick answer, there you have it!

However, there is a lot more when it comes to joining in Python. In this guide, you learn how joining/merging lists and other iterables work.

The join() method in Python

In Python, there is a built-in join() method that you can call on any string.

The join() method treats a string as a separator value for merging strings from an iterable.

Python join a list of strings

Syntax

string.join(iterable)

Parameters

The iterable is the only parameter to the join() method.

It can be any iterable type in Python, that is a list, tuple, string, dictionary, or set.

The only restriction is that the elements in the iterable must be strings. Otherwise merging them into one long string fails.

Return Value

The join() method returns a single string with the joined values from the iterable.

For example, you can use the join() method to merge the elements of a list into one string.

Example

For instance, given a list of number strings, let’s join them and separate them by letter “x”:

nums = ["5", "3", "10"]
joined = "x".join(nums)

print(joined)

Output:

5x3x10

In this piece of code:

  • The string “x” is a separator string.
  • We call the join() method and pass in the list of strings.
  • This turns the list of strings into one long string separated by the separator string.

This is a neat approach to merging a bunch of values into one string.

Examples: Joining Iterables in Python

In Python, common iterable data types are:

  • lists
  • tuples
  • strings
  • sets
  • dictionaries

When it comes to merging strings, you can pass any of these iterable types as an argument in the join() method call.

Let’s see examples of joining each of these iterable types.

Lists

At this point, joining lists of strings is nothing new.

For example:

nums = ["5", "3", "10"]
joined = "x".join(nums)

print(joined)

Output:

5x3x10

Tuples

A tuple is also an iterable type in Python.

Unlike lists, a tuple can be created simply by comma-separating elements.

For example, let’s join a tuple of strings into one sentence:

words = "Hello", "it", "is", "me"
joined = " ".join(words)

print(joined)

Output:

Hello it is me

Strings

In Python, a string is iterable. It is a sequence of characters.

Because a string is iterable, you can pass it as an argument to the join() method.

This can be handy if you want to separate characters in a string by some value.

For example:

word = "Hello"
joined = "-".join(word)

print(joined)

Output:

H-e-l-l-o

As another example, let’s print each character of a string into a separate line:

word = "Hello"
joined = "\n".join(word)

print(joined)

Output:

H
e
l
l
o

Sets

In Python, a set is an unordered group of elements.

A set is iterable.

Because there is no order, the elements of a set are randomly picked up from the set.

For example, let’s merge a set of words together:

word = {"Hello", "this", "is", "a", "test"}
joined = "-".join(word)

print(joined)

Output:

is-this-test-Hello-a

As you can see, the ordering is “incorrect”. This is because the notion of order is meaningless in a set as it is unordered.

Dictionaries

In Python, a dictionary is an iterable collection of key-value pairs.

This means you can also join a dictionary into a string.

But unlike other iterables, one dictionary element consists of two items — a key and a value.

How to Join Dictionary Keys

When joining a dictionary into a string, only the keys are taken into account.

For example:

d = {"Hello": 1, "this": 2, "is": 3, "a": 4, "test": 5}
joined = "-".join(d)

print(joined)

Output:

Hello-this-is-a-test

How to Join Dictionary Values

Joining the values is straightforward. Instead of calling the join() method on a dictionary as-is, you need to call it on the values.

To join the dictionary values into a string:

  1. Specify a separator string.
  2. Access the dictionary values using the dict.values() method.
  3. Call the join method on the separator string by passing the dictionary values as an argument.

For example:

d = {"Hello": "1", "this": "2", "is": "3", "a": "4", "test": "5"}
joined = "-".join(d.values())

print(joined)

Output:

1-2-3-4-5

Conclusion

Today you learned how to merge iterable elements into a single string in Python.

To recap, Python string implements the join() method. You can pass it an iterable argument. The join method loops through the elements of the iterable and combines them into a single string.

Thanks for reading!

Happy coding!

Further Reading

50 Best Websites to Learn Coding