Split and join a string in Python
Last Updated :
01 May, 2025
The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together with a different delimiter like a hyphen (-) to create a formatted string. The result would be "Hello,-how-are-you?".
Using split() and join()
split() function divides the string into a list of words and join() reassembles them with a specified separator. It is highly efficient and commonly used in Python for basic string manipulations.
Python
a = "Hello, how are you?"
b = a.split() # Split by space
c = "-".join(b) # Join with hyphen
print(b)
print(c)
Output['Hello,', 'how', 'are', 'you?']
Hello,-how-are-you?
Explanation:
- split() divides the string into a list of words, split by spaces by default.
- "-".join(b) reassembles the list into a string with hyphens between the words.
Using re.split() and '-'.join()
In cases needing advanced splitting e.g., handling multiple spaces or different delimiters, re.split() from the re module offers more flexibility. However, it’s less efficient than split() for simple cases due to the overhead of regular expression processing.
Python
import re
a = "Hello, how are you?"
b = re.split(r'\s+', a) # Split by spaces
c = "-".join(b) # Join with a hyphen
print(b)
print(c)
Output['Hello,', 'how', 'are', 'you?']
Hello,-how-are-you?
Explanation:
- re.split(r'\s+', a) split the string by one or more spaces, providing more flexibility for advanced splitting.
- "-".join(b) joins the list of words with hyphens.
Using str.partition() and str.replace()
This method splits the string manually using partition(), iterating over it to separate head and tail parts. After splitting, replace() or manual manipulation joins the parts. While functional, it’s less efficient due to multiple iterations and extra logic.
Python
a = "Hello, how are you?"
words, rem = [], a
# Split by space manually
while rem:
head, _, rem = rem.partition(" ")
if head: words.append(head)
c = "-".join(words) # Join with hyphen
print(words)
print(c)
Output['Hello,', 'how', 'are', 'you?']
Hello,-how-are-you?
Explanation:
- partition(" ") splits the string into three parts, before the first space (head), the space itself (sep) and after the space (tail). This process repeats until the string is fully split.
- Loop ensures all words are extracted and added to the list words.
Using str.split() with list comprehension
This method splits the string with split() and uses a list comprehension to process or filter the list. While more compact and readable, it adds an extra step, reducing efficiency compared to using split() and join() directly.
Python
a = "Hello, how are you?"
# Split by space
b = [word for word in a.split()]
# Join with a hyphen
c = "-".join(b)
print(b)
print(c)
Output['Hello,', 'how', 'are', 'you?']
Hello,-how-are-you?
Explanation:
- [word for word in a.split()] splits a by spaces into a list of words .
- "-".join(b) joins the words in b with hyphens.
Related Articles
Program to split and join a string
Similar Reads
Split and Parse a string in Python In this article, we'll look at different ways to split and parse strings in Python. Let's understand this with the help of a basic example:Pythons = "geeks,for,geeks" # Split the string by commas res = s.split(',') # Parse the list and print each element for item in res: print(item)Outputgeeks for g
2 min read
How to Index and Slice Strings in Python? In Python, indexing and slicing are techniques used to access specific characters or parts of a string. Indexing means referring to an element of an iterable by its position whereas slicing is a feature that enables accessing parts of the sequence.Table of ContentIndexing Strings in PythonAccessing
2 min read
Python String split() Python String split() method splits a string into a list of strings after breaking the given string by the specified separator.Example:Pythonstring = "one,two,three" words = string.split(',') print(words) Output:['one', 'two', 'three']Python String split() Method SyntaxSyntax: str.split(separator, m
6 min read
Split a string on multiple delimiters in Python In this article, we will explore various methods to split a string on multiple delimiters in Python. The simplest approach is by using re.split().Using re.split()The re.split() function from the re module is the most straightforward way to split a string on multiple delimiters. It uses a regular exp
2 min read
Split a string in equal parts (grouper in Python) Grouper recipe is an extended toolset made using an existing itertool as building blocks. It collects data into fixed-length chunks or blocks. Existing Itertools Used: izip_longest(*iterables[, fillvalue]) : Make an iterator that aggregates elements from each of the iterables. If the iterables are o
2 min read
Python String rsplit() Method Python String rsplit() method returns a list of strings after breaking the given string from the right side by the specified separator. It's similar to the split() method in Python, but the difference is that rsplit() starts splitting from the end of the string rather than from the beginning. Exampl
3 min read