Splitting & Joining Strings in Python

By Hemanta Sundaray on 2021-08-29

split()

We can use the split() method to split a string into a list of smaller strings based on some delimiter.

If we don’t pass any delimiter argument to the string() method, it will use any sequence of white space characters - newlines, spaces and tabs - to split the string.

data = "Python Flask Django"

print(data.split())
# ['Python', 'Flask', 'Django']

Passing a delimiter to the split() method:

data = "Python-Flask-Django"

print(data.split("-"))
# ['Python', 'Flask', 'Django']

partition()

We can split a string based on a separator string using the partition() method, which returns a tuple with three elements: the substring before the first instance of the separator string, the separator string itself and the substring after the separator string.

name = "Hemanta kumar sundaray"

print(name.partition('kumar'))
# ('Hemanta ', 'kumar', ' sundaray')

join()

We can join a list of strings into a single string using the join() method.

First, we specify the string that will glue the strings together and then call the join() method, passing it the list of strings that we want to join together as the argument.

empire = ['Python', 'Flask', 'Django']

print(", ".join(empire))
# Python, Flask, Django
Learn how to find and replace substrings in Python in my blog post here.

Join the Newsletter