Python program to split and join a string?

We'll learn how to split and join a string in Python in this article. Python defines strings as a collection of characters enclosed in one, two, or three quotations.

When a string is split, it is divided into smaller strings using a specific delimiter. A set of one or more characters used to define a boundary is known as a delimiter. The comma (,), semicolon (;), tab (\t), space ( ), and pipe (|) are the most widely used delimiters.

Input-Output Scenario

Following is an input and its output scenario to demonstrate the split and join of a string in Python ?

# Splitting the strings into list of strings
Input: Welcome to TutorialsPoint
Output: ['Welcome', 'to', 'TutorialsPoint']

# Using the delimiter ('-'), join the list of strings into a single string
Input: ['Welcome', 'to', 'TutorialsPoint']
Output: Welcome-to-TutorialsPoint

As we can see that at first the string is split into a list of strings and then combined with the help of the delimiter.

Using split() Method

The split() method breaks the given string by the specified separator and returns a list of strings ?

text = "Welcome to TutorialsPoint"
words = text.split()  # Split by whitespace (default)
print("Split by space:", words)

# Split by specific delimiter
data = "apple,banana,orange"
fruits = data.split(',')
print("Split by comma:", fruits)
Split by space: ['Welcome', 'to', 'TutorialsPoint']
Split by comma: ['apple', 'banana', 'orange']

Using join() Method

The join() method connects the elements of a sequence into a string using the specified separator ?

words = ['Welcome', 'to', 'TutorialsPoint']

# Join with hyphen
result1 = '-'.join(words)
print("Joined with hyphen:", result1)

# Join with space
result2 = ' '.join(words)
print("Joined with space:", result2)

# Join with no separator
result3 = ''.join(words)
print("Joined with no separator:", result3)
Joined with hyphen: Welcome-to-TutorialsPoint
Joined with space: Welcome to TutorialsPoint
Joined with no separator: WelcometoTutorialsPoint

Complete Split and Join Example

Here's a complete example that demonstrates both splitting and joining operations ?

def split_and_join_string(text, split_delimiter=' ', join_delimiter='-'):
    """Split a string and join it back with a different delimiter"""
    # Split the string
    word_list = text.split(split_delimiter)
    print(f"After splitting: {word_list}")
    
    # Join the list back into a string
    joined_string = join_delimiter.join(word_list)
    print(f"After joining: {joined_string}")
    
    return joined_string

# Example usage
original_text = "Once in a blue moon"
result = split_and_join_string(original_text, ' ', '-')
After splitting: ['Once', 'in', 'a', 'blue', 'moon']
After joining: Once-in-a-blue-moon

Different Delimiters Example

Let's see how to work with various delimiters ?

# Working with different delimiters
data = "name:John;age:25;city:New York"

# Split by semicolon
parts = data.split(';')
print("Split by semicolon:", parts)

# Further split each part by colon
key_value_pairs = []
for part in parts:
    key, value = part.split(':')
    key_value_pairs.append(f"{key}={value}")

# Join with comma
result = ', '.join(key_value_pairs)
print("Final result:", result)
Split by semicolon: ['name:John', 'age:25', 'city:New York']
Final result: name=John, age=25, city=New York

Conclusion

The split() method breaks strings into lists using delimiters, while join() combines list elements into strings. These methods are essential for text processing and data manipulation in Python.

Updated on: 2026-03-24T20:51:47+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements