Signup/Sign In

Python program to split and join a string

In this tutorial, we will learn to split and join a string in Python. Strings in Python are a sequence of characters wrapped in single, double, or triple quotes.

Splitting a string means breaking the string into substrings based on a certain delimiter. A delimiter is a sequence of one or more characters for specifying the boundary. A delimiter could be anything.

The most common delimiters that are used: a comma (,), a semicolon (;), a tab (\t), space ( ), and a pipe (|). We have to separate a given string wherever the specified delimiter is present and then join the substrings together.

Let us look at the sample input and output of the program.

Input: Welcome to study tonight

Output: Welcome-to-study-tonight

Input: Hello World

Output: Hello-World

To solve this problem in Python, we will use the split() and join() mehods of the string class in Python.

The split() method will return a list of strings after breaking the given string by the specified separator

The join() method returns a string in which the elements of a sequence have been joined by str separator.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Define a function that will accept a string and split the string

Step 2- In the function, declare a variable that will store the list of substrings returned by split()

Step 3- Return the variable

Step 4- Define another function that will accept a list of substrings and join them to form a string

Step 5- In the function, declare a string variable that will store the joined string

Step 6- Declare a string with characters

Step 7- Call the defined split function and pass the string

Step 8- Print value returned by the function

Step 9- Call the defined join function and pass the list

Step 10- Print value returned by the function

Python Program

Look at the program to understand the implementation of the above-mentioned approach. We have defined two functions, one for splitting the string and the other for joining the string.

def split_string(string):
    # Splitting based on space delimiter
    list_string = string.split(' ')
    return list_string

def join_string(list_string):
    # Joining based on '-' delimiter
    string = '-'.join(list_string)
    return string

string = 'Welcome to study tonight'
# Splitting a string
list_string = split_string(string)
print("After Splitting: ",list_string)

# Join list of strings into one
res_string = join_string(list_string)
print("After joining: ",res_string)


After Splitting: ['Welcome', 'to', 'study', 'tonight']
After joining: Welcome-to-study-tonight

Conclusion

In this tutorial, we have seen how to split and join a string in Python using the built-in functions of the string class. We have used the split() and join() methods in our program.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.