Table of contents
1.
Introduction 
2.
Python Join List
2.1.
Python
2.2.
Python
3.
Python join two strings
3.1.
Python
3.2.
Python
3.3.
Python
4.
Why join() function is in String and not in List?
4.1.
Python
5.
Joining list of multiple data-types
5.1.
Python
5.2.
Python
6.
Split String using join function
6.1.
Python
6.2.
Python
7.
Using split() function:
7.1.
Example 1: Splitting a string based on a single character delimiter
7.2.
Python
7.3.
Example 2: Splitting a string based on whitespace
7.4.
Python
7.5.
Example 3: Splitting a string with a limited number of splits
7.6.
Python
8.
Splitting only n times
8.1.
Python
8.2.
Python
8.3.
Python
9.
Frequently Asked Questions
9.1.
Can we join lists containing elements of different data types?
9.2.
What happens if we don't specify a separator in the join() method?
9.3.
Is it possible to split a string only a certain number of times using split()?
10.
Conclusion
Last Updated: Aug 8, 2024
Easy

Python String join() Method

Author Riya Singh
0 upvote

Introduction 

In Python, joining lists is a basic task that allows you to combine multiple lists into a single list. It's an easy process that can be achieved with the help of different methods. Joining lists is useful when you need to merge data from different sources or combine related elements. 

Python String join() Method

In this article, we'll learn different techniques to join lists in Python, like using the + operator, the extend() method, and the * operator. 

Python Join List

In Python, you can join two or more lists using the + operator. The + operator concatenates the lists, creating a new list that contains all the elements from the original lists in the order they appear. 

For example:

  • Python

Python

list1 = [1, 2, 3]

list2 = [4, 5, 6]

joined_list = list1 + list2

print(joined_list)
You can also try this code with Online Python Compiler
Run Code


Output

[1, 2, 3, 4, 5, 6]


In this example, we have two lists, list1 and list2. We use the + operator to join them together and assign the result to a new variable called joined_list. The resulting joined_list contains all the elements from list1 followed by all the elements from list2.

It's important to note that the + operator creates a new list and doesn't modify the original lists. If you want to join multiple lists, you can chain the + operator:

  • Python

Python

list1 = [1, 2, 3]

list2 = [4, 5, 6]

list3 = [7, 8, 9]

joined_list = list1 + list2 + list3

print(joined_list)
You can also try this code with Online Python Compiler
Run Code


Output

[1, 2, 3, 4, 5, 6, 7, 8, 9]


In this case, the + operator joins list1, list2, and list3 together in the order they appear, resulting in a new list containing all the elements from the three lists.

Python join two strings

In Python, you can join two strings using the join() method. The join() method is a string method that takes an iterable (such as a list) as an argument and joins the elements of the iterable into a single string, using the string on which join() is called as the separator. 

For example

  • Python

Python

string1 = "Hello"

string2 = "World"

joined_string = " ".join([string1, string2])

print(joined_string)
You can also try this code with Online Python Compiler
Run Code

 

Output

"Hello World"


In this example, we have two strings, string1 and string2. We create a list containing these two strings and pass it as an argument to the join() method. The join() method is called on a space character " ", which acts as the separator between the elements. The resulting joined_string is a single string where string1 and string2 are joined with a space in between.

You can use any string as the separator when joining strings. For example, you can use a comma:

  • Python

Python

string1 = "Hello"

string2 = "World"

joined_string = ",".join([string1, string2])

print(joined_string)
You can also try this code with Online Python Compiler
Run Code


Output

"Hello,World"


In this case, the strings are joined with a comma as the separator.

The join() method is not limited to joining only two strings. You can join any number of strings by providing them as elements in the iterable. For example:

  • Python

Python

strings = ["Hello", "World", "Python", "Programming"]

joined_string = " ".join(strings)

print(joined_string)
You can also try this code with Online Python Compiler
Run Code


Output

"Hello World Python Programming"


Here, we have a list of strings called strings, and we join them together using the join() method with a space as the separator. The resulting joined_string contains all the strings joined with spaces in between.

Why join() function is in String and not in List?

In Python, the join() method is a string method and not a list method. This design decision may seem counterintuitive at first, but there are good reasons behind it.

The join() method is used to join the elements of an iterable (such as a list) into a single string, using the string on which join() is called as the separator. The key point here is that the result of joining elements is always a string, not a list.

If the join() method were a list method, it would imply that the result of joining elements would be a list, which is not the case. Joining elements of a list results in a string, and it makes more sense for the join() method to be a string method.

Moreover, having the join() method as a string method allows for more flexibility and readability. You can choose any string as the separator when joining elements, which gives you control over the formatting of the resulting string. For example:

  • Python

Python

my_list = ["Messi", "Ronaldo", "Kohli"]

joined_string = ", ".join(my_list)

print(joined_string)
You can also try this code with Online Python Compiler
Run Code


Output: 

"Messi, Ronaldo, Kohli"


In this example, we use a comma followed by a space (", ") as the separator when joining the elements of my_list. The resulting joined_string is a well-formatted string with the elements separated by commas and spaces.

If the join() method were a list method, the syntax would be less intuitive and readable. You would have to specify the separator separately, and the intention of joining elements into a string would be less clear.

Moreover, if you have the join() method as a string method aligns with the principle of "string concatenation" in Python. Joining elements of an iterable into a string is a form of string concatenation, and it makes sense to have this functionality associated with strings.

Joining list of multiple data-types

In Python, you can join lists containing elements of different data types using the join() method. However, there's a catch: 

The join() method expects all the elements in the iterable to be strings. If the list contains elements of different data types, you need to convert them to strings before joining them.

For example : 

  • Python

Python

my_list = [1, "Hello", True, 3.14]

joined_string = " ".join(str(item) for item in my_list)

print(joined_string)
You can also try this code with Online Python Compiler
Run Code

Output

"1 Hello True 3.14"


In this example, my_list contains elements of different data types: an integer (1), a string ("Hello"), a boolean (True), and a float (3.14). To join these elements into a string, we use a list comprehension (str(item) for item in my_list) to convert each element to a string using the str() function. The join() method is then called on a space character (" ") to join the string elements with spaces in between.

Alternatively, you can use a list comprehension to convert the elements to strings and then join them:

  • Python

Python

my_list = [1, "Hello", True, 3.14]

string_list = [str(item) for item in my_list]

joined_string = " ".join(string_list)

print(joined_string)
You can also try this code with Online Python Compiler
Run Code


Output

"1 Hello True 3.14"


In this approach, we first use a list comprehension to convert each element of my_list to a string, creating a new list called string_list. Then, we join the elements of string_list using the join() method with a space as the separator.

It's important to note that when converting elements to strings, the str() function is used, which returns the string representation of the element. For custom objects, you can define the __str__() method to specify how the object should be represented as a string.

Split String using join function

While the join() method is primarily used to join elements of an iterable into a string, you can also use it in combination with the split() method to split a string into a list of substrings based on a specific separator.

Let’s discuss an example of splitting a string using the split() method and then joining the resulting substrings using the join() method:

  • Python

Python

my_string = "Hello,World,How,Are,You"

substrings = my_string.split(",")

print(substrings)

joined_string = " ".join(substrings)

print(joined_string)
You can also try this code with Online Python Compiler
Run Code


Output: 

['Hello', 'World', 'How', 'Are', 'You']
"Hello World How Are You"


In this example, we start with a string my_string that contains comma-separated values. We use the split() method with a comma (",") as the separator to split the string into a list of substrings. The resulting substrings list contains the individual substrings without the commas.

Next, we use the join() method to join the substrings back into a single string. We specify a space (" ") as the separator to insert a space between each substring. The resulting joined_string contains the substrings joined together with spaces.

This combination of split() and join() methods can be useful when you want to manipulate or reformat a string by splitting it into substrings, modifying the substrings if needed, and then joining them back together with a different separator.

For example : 

  • Python

Python

my_string = "apple,banana,cherry,date"

substrings = my_string.split(",")

modified_substrings = [substring.capitalize() for substring in substrings]

joined_string = "-".join(modified_substrings)

print(joined_string)
You can also try this code with Online Python Compiler
Run Code


Output

"Apple-Banana-Cherry-Date"


In this example, we split the my_string into substrings using the comma separator. Then, we use a list comprehension to capitalize each substring using the capitalize() method. Finally, we join the modified substrings using the join() method with a hyphen ("-") as the separator.

Using split() function:

The split() function in Python is used to split a string into a list of substrings based on a specified delimiter. It provides a convenient way to break a string into smaller parts for further processing or manipulation.

The basic syntax of the split() function is:

string.split(separator, maxsplit)

 

- `separator`: The delimiter used to split the string. It can be a single character or a substring. If not specified, the default separator is whitespace (spaces, tabs, newlines).

- `maxsplit`: An optional integer that specifies the maximum number of splits to be performed. If not specified or set to -1, all possible splits are made.

For example : 

Example 1: Splitting a string based on a single character delimiter

  • Python

Python

my_string = "apple,banana,cherry,date"

substrings = my_string.split(",")

print(substrings)
You can also try this code with Online Python Compiler
Run Code

 

Output: 

['apple', 'banana', 'cherry', 'date']


In this example, we split the string my_string using a comma (",") as the delimiter. The split() function returns a list of substrings, where each substring represents a part of the original string separated by the comma.

Example 2: Splitting a string based on whitespace

  • Python

Python

my_string = "Hello World How Are You"

substrings = my_string.split()

print(substrings)
You can also try this code with Online Python Compiler
Run Code


Output

['Hello', 'World', 'How', 'Are', 'You']


In this case, we don't specify any delimiter, so the split() function uses whitespace as the default separator. The string is split wherever whitespace is encountered, resulting in a list of individual words.

Example 3: Splitting a string with a limited number of splits

  • Python

Python

my_string = "apple,banana,cherry,date"

substrings = my_string.split(",", maxsplit=2)

print(substrings)
You can also try this code with Online Python Compiler
Run Code


Output

['apple', 'banana', 'cherry,date']


Here, we use the maxsplit parameter to limit the number of splits to 2. The split() function splits the string at the first two occurrences of the comma delimiter and returns a list of three substrings. The remaining part of the string ("cherry,date") remains intact as a single substring.

The split() function is very useful when you have a string containing structured data, like comma-separated values (CSV) or tab-separated values (TSV), and you want to extract individual elements from the string.

It's important to note that the split() function returns a list of substrings, so you can further process or manipulate the resulting substrings as needed. You can access individual substrings using indexing, iterate over them using loops, or apply other list operations.

Splitting only n times

When using the split() function in Python, you can control the number of splits performed by specifying the maxsplit parameter. This allows you to split a string only a certain number of times, leaving the remaining part of the string intact.

For example : 

  • Python

Python

my_string = "Sinki,Rahul,Akash,Sanjana,Vaibhav"

substrings = my_string.split(",", maxsplit=3)

print(substrings)
You can also try this code with Online Python Compiler
Run Code


Output

['Sinul', 'Akash', 'Sanjana,Vaibhav']


In this example, we have a string my_string that contains comma-separated values. We use the split() function with the comma (",") as the delimiter and set maxsplit to 3. This means that the split() function will split the string at the first three occurrences of the comma, resulting in a list of four substrings.

The remaining part of the string ("Sanjana,Vaibhav") remains intact as a single substring, because the maxsplit limit of 3 has been reached.

You can adjust the value of maxsplit to control the number of splits performed:

  • Python

Python

my_string = "Sinki,Rahul,Akash,Sanjana,Vaibhav"

substrings = my_string.split(",", maxsplit=1)

print(substrings)
You can also try this code with Online Python Compiler
Run Code

 

Output:

['Sinki', 'Rahul,Akash,Sanjana,Vaibhav']


In this case, we set maxsplit to 1, so the split() function only splits the string at the first occurrence of the comma, resulting in a list of two substrings.

If you set maxsplit to 0 or don't specify it at all, the split() function will perform all possible splits, which is equivalent to splitting the string at every occurrence of the delimiter:

  • Python

Python

my_string = "Sinki', 'Rahul,Akash,Sanjana,Vaibhav"

substrings = my_string.split(",")

print(substrings)
You can also try this code with Online Python Compiler
Run Code

 

Output

['Sinki', 'Rahul', 'Akash', 'Sanjana', 'Vaibhav']


Here, the split() function splits the string at every comma, which gives the output in a list of five substrings.

The maxsplit parameter gives you control over the number of splits performed, allowing you to handle cases where you only want to split the string a certain number of times. This can be useful when you have structured data and want to extract specific parts of the string while keeping the remaining part intact.

Frequently Asked Questions

Can we join lists containing elements of different data types?

Yes, you can join lists with elements of different data types by converting them to strings using the str() function before joining.

What happens if we don't specify a separator in the join() method?

If no separator is specified, the join() method will concatenate the elements without any separator between them.

Is it possible to split a string only a certain number of times using split()?

Yes, you can use the maxsplit parameter in the split() function to specify the maximum number of splits to be performed.

Conclusion

In this article, we discussed the different methods to join lists in Python using the + operator, extend() method, and join() method. We also learned how to join strings using the join() method and how to split strings using the split() function. Every programmer should learn these techniques as they allows you to efficiently combine and manipulate lists and strings in your Python programs.

You can also check out our other blogs on Code360.

Live masterclass