Table of contents
1.
Introduction
2.
Split the String into a List of Strings
2.1.
Using string.split()
2.2.
Syntax
2.3.
Example 1: Splitting a String Using Default Whitespace
2.4.
Example 2: Splitting Using a Custom Delimiter
2.5.
Example 3: Limiting the Number of Splits
3.
Using splitlines()
3.1.
Syntax
3.2.
Example: Splitting a Multi-line String
4.
Join the List of Strings into a String
4.1.
Using join() Method
4.2.
Syntax
4.3.
Example 1: Joining a List into a String
4.4.
Example 2: Joining with a Custom Separator
4.5.
Example 3: Joining a List of Numbers (Requires Conversion)
5.
Using for Loop [Less Efficient]
5.1.
Example: Joining Using a for Loop
6.
Frequently Asked Questions
6.1.
What is the difference between split() and splitlines()?
6.2.
Why is join() preferred over a for loop for concatenation?
6.3.
Can we split a string using multiple delimiters?
7.
Conclusion
Last Updated: Mar 11, 2025
Medium

Python String split() and join() Methods

Author Rahul Singh
0 upvote

Introduction

Python provides the split() and join() methods to manipulate strings efficiently. The split() method breaks a string into a list based on a specified delimiter, making it useful for text processing. Conversely, the join() method combines elements of an iterable (like a list) into a single string using a specified separator. 

Python String split() and join() Methods

In this article, we will discuss the syntax, usage, and examples of split() and join() methods in Python.

Split the String into a List of Strings

Splitting a string means breaking it into multiple substrings based on a specific separator. Python provides different ways to achieve this.

Using string.split()

The split() method is the most commonly used function to break a string into a list of substrings based on a specified delimiter.

Syntax

string.split(separator, maxsplit)

 

  • separator: The delimiter on which the string is split (default is whitespace).
     
  • maxsplit: The maximum number of splits (optional).

Example 1: Splitting a String Using Default Whitespace

text = "Python is a powerful language"
words = text.split()
print(words)
You can also try this code with Online Python Compiler
Run Code


Output:

['Python', 'is', 'a', 'powerful', 'language']


Here, since no separator is specified, the string is split at whitespace.

Example 2: Splitting Using a Custom Delimiter

csv_data = "apple,banana,grapes,orange"
fruits = csv_data.split(",")
print(fruits)
You can also try this code with Online Python Compiler
Run Code


Output:

['apple', 'banana', 'grapes', 'orange']


The string is split at commas, resulting in a list of words.

Example 3: Limiting the Number of Splits

data = "name:age:city:country"
info = data.split(":", 2)
print(info)
You can also try this code with Online Python Compiler
Run Code


Output:

['name', 'age', 'city:country']


The string is split at : only twice, keeping the remaining part as a single element.

Using splitlines()

The splitlines() method is used to split a string at line breaks (\n). It is useful for processing multi-line strings.

Syntax

string.splitlines(keepends)
  • keepends: If set to True, it keeps newline characters in the output.

Example: Splitting a Multi-line String

multiline_text = "Hello\nWelcome to Python\nHave a great day!"
lines = multiline_text.splitlines()
print(lines)
You can also try this code with Online Python Compiler
Run Code


Output:

['Hello', 'Welcome to Python', 'Have a great day!']


The function breaks the string at newline characters and returns a list.

Join the List of Strings into a String

Joining a list means combining multiple elements into a single string with a specified separator.

Using join() Method

The join() method is the most efficient way to join elements of a list into a string.

Syntax

separator.join(iterable)

 

  • separator: The string that separates the elements in the resulting string.
     
  • iterable: A list, tuple, or other iterable containing strings.

Example 1: Joining a List into a String

words = ['Python', 'is', 'awesome']
sentence = " ".join(words)
print(sentence)
You can also try this code with Online Python Compiler
Run Code


Output:

'Python is awesome'


Each word in the list is joined using a space as a separator.

Example 2: Joining with a Custom Separator

fruits = ['apple', 'banana', 'cherry']
csv_format = ",".join(fruits)
print(csv_format)
You can also try this code with Online Python Compiler
Run Code


Output:

'apple,banana,cherry'


The list elements are joined using a comma.

Example 3: Joining a List of Numbers (Requires Conversion)

numbers = [1, 2, 3, 4]
numbers_str = "-".join(map(str, numbers))
print(numbers_str)
You can also try this code with Online Python Compiler
Run Code


Output:

'1-2-3-4'


Since numbers cannot be joined directly, they are converted to strings first.

Using for Loop [Less Efficient]

Although the join() method is preferred, a for loop can also be used to concatenate strings.

Example: Joining Using a for Loop

words = ['Hello', 'world', 'Python']
result = ""
for word in words:
    result += word + " "
print(result.strip())
You can also try this code with Online Python Compiler
Run Code


Output:

'Hello world Python'


However, this approach is inefficient as it repeatedly creates new string objects, increasing execution time.

Frequently Asked Questions

What is the difference between split() and splitlines()?

split() breaks a string based on a specified delimiter, while splitlines() splits at line breaks (\n). splitlines() is useful for multi-line text processing.

Why is join() preferred over a for loop for concatenation?

join() is optimized for string concatenation as it processes all elements in one step, whereas a for loop creates multiple intermediate strings, making it slower.

Can we split a string using multiple delimiters?

No, split only supports one delimiter at a time. However, you can use the re.split() function from the re module for splitting using multiple delimiters.

Conclusion

In this article, we discussed how to split and join strings in Python using various methods. The split() and splitlines() methods help break strings into lists, while the join() method efficiently combines lists into strings. Understanding these functions is crucial for text processing and data handling in Python

Live masterclass