Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 06

split() and join()

Learn how to break a string into a list with split() and rebuild it into a string using join() through visual diagrams and real-world examples.

Beginner25 min

What you'll learn

Topic Focus
split() Convert String → List
Custom delimiters -, #, $, /
Date parsing Extract day, month & year
join() Convert List → String

Visual roadmap

These two methods are opposites.

String → split() → List → join() → String

Golden rule: split() breaks · join() builds

1. split() → Default (spaces)

By default, split() separates words using whitespace.

Python is an oop lang split into word chips
python
s = "Python is an oop lang"

words = s.split()

print(words)
# ['Python', 'is', 'an', 'oop', 'lang']

print(len(words))
# 5
String ÷ Spaces = List memory formula

2. split(delimiter)

Provide a delimiter to split on a specific character.

08-07-2023 split by dash into day month year
python
dob = "08-07-2023"

parts = dob.split("-")

print(parts)
# ['08', '07', '2023']

print(parts[0])  # Day
print(parts[1])  # Month
print(parts[2])  # Year

3. Different delimiters

You can split using any separator.

# split — Apple Banana Kiwi
/ split — Apple Banana Kiwi
python
s = "Apple#Banana#Kiwi"

print(s.split("#"))
# ['Apple', 'Banana', 'Kiwi']

s = "Apple/Banana/Kiwi"

print(s.split("/"))
# ['Apple', 'Banana', 'Kiwi']

4. Trailing delimiter

A separator at the end creates an empty last item.

123$456$678$ — trailing $ yields empty string
python
s = "123$456$678$"

print(s.split("$"))
# ['123', '456', '678', '']

Warning: The last '' represents nothing after the final delimiter.

5. Nested splitting

Sometimes data contains multiple separators.

Apple#Mango-Kiwi#Guava — split # then split -
python
s = "Apple#Mango-Kiwi#Guava"

x = s.split("#")
print(x)

y = x[1].split("-")
print(y)

Output:

text
['Apple', 'Mango-Kiwi', 'Guava']
['Mango', 'Kiwi']

6. join() → Build a string

join() combines list or tuple items into one string.

List chips joined with space into sentence
python
lst = ["Python", "is", "an", "oop", "lang"]

sentence = " ".join(lst)

print(sentence)
# Python is an oop lang

Memory: join() = glue between list items

7. Different separators with join()

The string before .join() becomes the separator.

empty join vs space join
space join HYD BANG AP
python
cities = ["HYD", "BANG", "AP"]

print("".join(cities))
# HYDBANGAP

print(" ".join(cities))
# HYD BANG AP

Comma separated:

python
print(",".join(cities))
# HYD,BANG,AP

8. join() works with tuples

Any iterable works — not just lists.

Tuple + join → Rossum is Father of Python
python
t = ("Rossum", "is", "Father", "of", "Python")

print(" ".join(t))
# Rossum is Father of Python

9. split() ↔ join() relationship

These methods are exact opposites.

split down to list, join back to string
Direction Method Returns
String → Parts split() List
Parts → String join() String

Playground example

python
s = "Python is an oop lang"

words = s.split()

print(words, len(words))

dob = "08-07-2023"

parts = dob.split("-")

print("Day=", parts[0])
print("Month=", parts[1])
print("Year=", parts[2])

lst = ["Python", "is", "an", "oop", "lang"]

sentence = " ".join(lst)

print(sentence)

Expected output:

text
['Python', 'is', 'an', 'oop', 'lang'] 5

Day= 08
Month= 07
Year= 2023

Python is an oop lang

Use the lesson playground at the bottom of the page to run this code live.

Quick knowledge check

  1. What is the default delimiter for split()?
    Answer: Whitespace (spaces, tabs, newlines)

  2. What does "06-05-2024".split("-") return?
    Answer: ['06', '05', '2024']

  3. How do you join ['a', 'b', 'c'] with commas?
    Answer:

python
",".join(["a", "b", "c"])
# a,b,c

30-second visual revision

String Text hub — split, join, delimiter
Method Easy memory
split() Break a string into pieces
split("-") Parse dates
join() Glue list items together
" ".join() Build readable sentences

Interview shortcut: Split = String → List · Join = List → String · The string before join() is the separator.

Next: startswith() and endswith().

Loading playground…